Yii Framework Forum: Relation Between Models And Display In Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Relation Between Models And Display In Cgridview sql query that select the sum of the second table Rate Topic: ***** 1 Votes

#1 User is offline   nhexia 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 15-November 12

Posted 04 December 2012 - 03:07 AM

table 1
-----------
loader
-----------
bl_no PK
bl_cbm
bl_value


table 2
-------------
cargo
-------------
awb_no
bl_no
awb_cbm
awb_value

*****************************
1 loader = has many cargo
*****************************
-----------------------------------------------------------------------------------
Objective:
= get the sum of cargo.awb_cbm and cargo.awb_value
= after that, display the following in cgridview
-------------------------------------------------
| bl_no | bl_value | sum(cargo.awb_value) | Net |
-------------------------------------------
| DD142 | 5000     | 3000                 | 200 |
-------------------------------------------------

same thing in CBM, get the diffrence of the loader.bl_cbm and cargo.awb_cbm

i use this query and im success to get all i need
SELECT a.*,
        (SELECT SUM(b.cbm) as ccbm FROM cargo b
    	   WHERE b.bl_no = a.bl_no
        ) as cbm_difference,
        (SELECT SUM(b.awb_value) as selling FROM cargo b
    	WHERE b.bl_no = a.bl_no) NET
FROM loader a

-----------------------------------------------------------------------------------
my problem is:
i dont now how to make a function that can query and display in cgridview, i dont know if this is right?
my relation model

-----------------------------------------------------------------------------------
Model : loader
public function relations()
	{
		

		return array(
                          "x" => array(self::HAS_MANY, "cargo", "bl_no",),
		);
                
	}

-----------------------------------------------------------------------------------

please anyone can help T_T.... im tired to find out
thanks in advance, sorry for my english low...

Reinhard Navarro
0

#2 User is offline   robregonm 

  • Expert Yii Developer
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 591
  • Joined: 30-July 09
  • Location:Colombia

Posted 04 December 2012 - 11:42 AM

First of all CGridView uses DataProviders, so you can put your SQL code into a CSQLDataProvider object and the use it (filter, etc)
Ricardo Obregón

YiiFramework en Español - http://yiiframework.co/ - http://yiiframeworkenespanol.org/ - Yii Code Generator for Bootstrap
http://obregon.co/ - https://1server.co/
PHP 5.5+, nginx 1.4, MySQL(MariaDB & PerconaDB), PostgreSQL 9, Yii 2.0-dev.
Follow me: @robregonm & @obregonco & @1ServerCo.
0

#3 User is offline   robregonm 

  • Expert Yii Developer
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 591
  • Joined: 30-July 09
  • Location:Colombia

Posted 04 December 2012 - 11:45 AM

On the other hand you cannot create relations to SQL code.
If you need to calculate some children fields you might need CActiveRecord::STAT queries/relations.
Hope this helps.
Ricardo Obregón

YiiFramework en Español - http://yiiframework.co/ - http://yiiframeworkenespanol.org/ - Yii Code Generator for Bootstrap
http://obregon.co/ - https://1server.co/
PHP 5.5+, nginx 1.4, MySQL(MariaDB & PerconaDB), PostgreSQL 9, Yii 2.0-dev.
Follow me: @robregonm & @obregonco & @1ServerCo.
0

#4 User is offline   nhexia 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 15-November 12

Posted 04 December 2012 - 10:14 PM

Allowed memory size of 8388608 bytes exhausted (tried to allocate 14592 bytes) in /var/www/html/monitoring/protected/extensions/bootstrap/widgets/TbBaseMenu.php on line 149


i goty another problem help please
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users