Imprimir campos a traves de una relación

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!!!

El error dice que no existe el campo postId en la tabla categories… cosa que esta correcta porque tienes definido categoriesId y name como campos…

Supongo que no le has hechado un vistazo a http://www.yiiframework.com/doc/guide/1.1/en/database.arr porque ahi sale la misma definicion de la BD y como se utilizan las tablas intermedias…

Ahora ya que decidiste crear un modelo para la tabla intermedia PostsCategories podrias crear una relacion de categorias hacia PostsCategories y post hacia PostsCategories …