Hola
Estoy dando mis primeros pasos con Yii y tengo varias dudas. Ando haciendo un pequeño blog y por ahora me interesa crear blogs y darles categorias. He podido hacer que cuando se crea un blog este se asocie a varias categorias, la tabla intermedia se llena correctamente.
El problema es que NO puedo recuperar las categorias a las que pertenece un blog. Que puedo hacer?
Mi base de datos
Posts
-------
postId(PK)
name
Categories
-------
categoriesId(PK)
name
PostsCategories
-------
categoriesId(FK)
postID(FK)
Mis relaciones en la base de datos
categoriesId.mydb.categories.categoriesId
postId.mydb.posts.postId
Relaciones de mi modelo Posts
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'categories' => array(self::HAS_MANY, 'Categories', 'postId'),
);
}
Relacion de Categories
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'Posts' => array(self::HAS_MANY, 'Posts', 'categoriesId'),
);
}
Relaciones PostsCategories
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'categories' => array(self::BELONGS_TO, 'Categories', 'categoriesId'),
'post' => array(self::BELONGS_TO, 'Posts', 'postId'),
);
}
PostController
public function loadModel($id)
{
$model=Posts::model()->findByPk((int)$id);
$criteria=new CDbCriteria;
$criteria->condition='postId=:postId';
$criteria->select = 'categoriesId';
$criteria->params=array(':postId'=>$_GET['id']);
$postCategories = PostsCategories::model()->findAll($criteria);
$categories = array();
foreach ($postCategories as $category) {
$categories[] = $category->categoriesId;
}
if($model===null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
Y este es mi _view.php
<b><?php echo $items = Posts::model()->with('categories')->findAll();?>
<br />
Cuando intento ver un post determinado Yii me devuelve esto
The relation "categories" in active record class "Posts" is specified with an invalid foreign key "postId". There is no such column in the table "Categories".
Y no se donde esta el error.
gracias!!!