Yii Framework Forum: AR - Join - Yii Framework Forum

Jump to content

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

AR - Join Access columns in JOIN statements Rate Topic: -----

#1 User is offline   Scryii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 30-September 10
  • Location:Germany, Munich

Posted 31 October 2010 - 02:19 AM

I try to get data from a JOINed table:

$crit = new CDbCriteria();
$crit->condition = 'page > :page';
$crit->params = array(':page'=>0);
$crit->limit = 20;
$crit->join = 'LEFT JOIN table_2 ON table_2.id=page';

$rows=$ar->findAll($crit);

Ok, that works. In table_2 I have a colum called 'title'
I access all data in a foreach loop

foreach ($rows as $row)

That works too.

But the following code makes an exception

echo $row->title;

because (imho) this is table_2

But how can I access columns of the JOINed table ?


Thx 4 help

Scryii
0

#2 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

Posted 31 October 2010 - 02:44 AM

You can print the $row and you will see where your data is, I think you can access this title throug something like this $row->table2->title

But try to print $row and see where the data is
0

#3 User is offline   Scryii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 30-September 10
  • Location:Germany, Munich

Posted 31 October 2010 - 02:57 AM

View Postignis, on 31 October 2010 - 02:44 AM, said:

You can print the $row and you will see where your data is, I think you can access this title throug something like this $row->table_2->title

But try to print $row and see where the data is




Thx for help.

Did you mean the following code ?

print $row;

That doesn't work.

Also the access via $row->table_2->title doesn'n work.

I still get the exception:

Property "MyApplication.table_2" is not defined.
0

#4 User is offline   ignis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 57
  • Joined: 27-April 10
  • Location:Lithuania - Vilnius

Posted 31 October 2010 - 03:02 AM

try to use with() method for table joinings.

Read this article http://www.yiiframew...en/database.arr and join the tables by setting relational property

You can check generated query in yii log and you will see what she is getting
0

#5 User is offline   Scryii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 30-September 10
  • Location:Germany, Munich

Posted 31 October 2010 - 03:25 AM

That looks good.
I'm sure that is the right way, I will check it.

On the other site it looks more complex.
I have only one AR for the table_1.
To follow your idea, I have to generate an AR for table_2 too,
because relations are only possible btw. AR.

The using of the join property in an AR is very simple and generates the
right table (in my opinion => I tested in phpMyAdmin).

Is there really not a simple way to access the JOINed columns ?
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 31 October 2010 - 03:29 AM

View PostScryii, on 31 October 2010 - 03:25 AM, said:

That looks good.
I'm sure that is the right way, I will check it.

On the other site it looks more complex.
I have only one AR for the table_1.
To follow your idea, I have to generate an AR for table_2 too,
because relations are only possible btw. AR.

The using of the join property in an AR is very simple and generates the
right table (in my opinion => I tested in phpMyAdmin).

Is there really not a simple way to access the JOINed columns ?

You can add 'title' property to your model:

class MyModel extends CActiveRecord
{
    public $title;

    // ...
}

But Yii can do all job for you, if you'll create AR class for table_2 and add a relation to your model.
0

#7 User is offline   Scryii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 30-September 10
  • Location:Germany, Munich

Posted 31 October 2010 - 12:57 PM

Ok, thanks, let us do this step by step:

1. I Generate the model class for table_2 using Gii => ok
2. I added to the model class of table_1 (function relation) the line:

'name'=>array(self::BELONGS_TO, 'Table2Class', 'id'),

=> ok

3. Then I changed the call:

$rows=$ar->findAll($crit);

to

$rows=$ar->with('name')->findAll($crit);

=> ok

4. And now the question: How I can access the 'title' colum of table_2 ?

echo $row->title;

doesn't work (exception)

scryii

P.S.: When I copy the logged SQL Statement to myPHP admin => the JOINed table looks perfect. It's only the last step - the access to the colum.
0

#8 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 31 October 2010 - 01:21 PM

View PostScryii, on 31 October 2010 - 12:57 PM, said:

Ok, thanks, let us do this step by step:

1. I Generate the model class for table_2 using Gii => ok
2. I added to the model class of table_1 (function relation) the line:

'name'=>array(self::BELONGS_TO, 'Table2Class', 'id'),

=> ok

3. Then I changed the call:

$rows=$ar->findAll($crit);

to

$rows=$ar->with('name')->findAll($crit);

=> ok

4. And now the question: How I can access the 'title' colum of table_2 ?

echo $row->title;

doesn't work (exception)

scryii

P.S.: When I copy the logged SQL Statement to myPHP admin => the JOINed table looks perfect. It's only the last step - the access to the colum.

What's about $row->name->title?
0

#9 User is offline   Scryii 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 54
  • Joined: 30-September 10
  • Location:Germany, Munich

Posted 31 October 2010 - 01:40 PM

Thanx.

That works.

B)
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