Problem With Many_Many Relations

In Goods model:

public function relations() {


    return array(


        'pg' => array(self::MANY_MANY, 'Project', '{{project_goods}}(pid, gid)'),

$model = Goods::model()->with(‘pg’)->findByPk(2);

    echo $model->pg.name;

Use of undefined constant name - assumed ‘name’




echo $model->pg->name;



thank you.

echo $model->pg->name

Trying to get property of non-object

$model = Goods::model()->with("pg")->findByPk(2);

    $model->dbCriteria->addCondition("pg.pid=2");


    echo $model->pg->name;

> Info: If there is no related instance for a relationship, the

corresponding property could be either null or an empty array. For

BELONGS_TO and HAS_ONE relationships, the result is null; for

HAS_MANY and MANY_MANY, it is an empty array.

Note that the HAS_MANY and MANY_MANY relationships return arrays of objects,

you will need to loop through the results before trying to access any properties.

Otherwise, you may receive "Trying to get property of non-object" errors.

hope to help other people

$model->pg[0]->name

it’s ok

Yes, since it’s a many-many relationship, $model->pg will return an array of Projects.




foreach ($model->pg as $project)

{

    echo $project->name;

}



Matt