ease
(Qzhlan)
1
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’
ease
(Qzhlan)
3
thank you.
echo $model->pg->name
Trying to get property of non-object
ease
(Qzhlan)
4
$model = Goods::model()->with("pg")->findByPk(2);
$model->dbCriteria->addCondition("pg.pid=2");
echo $model->pg->name;
ease
(Qzhlan)
5
> 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
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