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'
Page 1 of 1
Problem With Many_Many Relations 'Project', '{{project_goods}}(pid, gid)
#3
Posted 15 November 2012 - 10:38 PM
thank you.
echo $model->pg->name
Trying to get property of non-object
echo $model->pg->name
Trying to get property of non-object
#4
Posted 15 November 2012 - 10:44 PM
$model = Goods::model()->with("pg")->findByPk(2);
$model->dbCriteria->addCondition("pg.pid=2");
echo $model->pg->name;
$model->dbCriteria->addCondition("pg.pid=2");
echo $model->pg->name;
#5
Posted 15 November 2012 - 11:31 PM
> 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
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
#7
Posted 16 November 2012 - 06:56 AM
Yes, since it's a many-many relationship, $model->pg will return an array of Projects.
Matt
foreach ($model->pg as $project) { echo $project->name; }
Matt
Share this topic:
Page 1 of 1