Yii:Show Published Post And Approved Comment at RecentComments List - php

I used yii blog from Yii Framework
I want Show Only Published Post And Approved Comment at RecentComments List
I used This code at Comment Model
public function findRecentComments($limit=10)
{
return $this->with('post')->findAll(array(
'condition'=>'t.status='.self::STATUS_APPROVED.'status='.Post::STATUS_PUBLISHED,
'order'=>'t.create_time DESC',
'limit'=>$limit,
));
}
But Show All Post And Approved Comment in RecentComments List
I want Show Published Post And Approved Comment at RecentComments List

Setting condition for relation model is done within with() function.
I think findAllByAttributes() is more readible so here's solution using this function
public function findRecentComments($limit=10)
{
return $this->with(array('post' => array(
'condition' => 'post.status=:status',
'params' => array(':status' => Post::STATUS_PUBLISHED),
)))->findAllByAttributes( array(
self::getTableAlias(false, false).'.status' => self::STATUS_APPROVED
),
array(
'order'=> self::getTableAlias(false, false).'.create_time',
'limit' => $limit
) );
}

Related

Different current page in one controller

I am using pagination on my CakePHP page and it works well, but I have a few pages generated by different functions in one controller file and there is an cumbersome issue.
Let's assume I've got two tables with hundreds of entries, which my paginators divide and display 15 of entries on one page:
public $paginate = array(
'Table1' => Array(
'order' => array('Table1.id' => 'desc'),
'limit' => 15
),
'Table2' => Array(
'order' => array('Table2.id' => 'asc'),
'limit' => 15
)
);
And functions in this controller file:
public function showTable1() {
$this->set('table', $this->paginate('Table1'));
}
public function showTable2() {
$this->set('table', $this->paginate('Table2));
}
When I go to the next page while displaying "Table 1", I go to the next page and it works well. But when I open another page with "Table 2" generated by another function from the same controller file, it unfortunately also goes to the second page.
What should I do to have different "pages indicators"? I mean, when I go to the third page on "Table 1", I don't want to go to the third page when I go to "Table 2".
Thanks in advance. I hope I presented my problem clearly.
instead of setting the pagination options on the top of the controller, try deleting that public $paginate and setting all the options inside action, like this (and in the similar way for the second action)
public function showTable1() {
$options = array(
'conditions' => array(
'Table1.id' => whatever
),
'fields' => array(
'Table1.id'
),
'order' => array(
'Table1.id' => 'DESC'
),
'limit' => 15
);
$this->Paginator->settings = $options;
$table = $this->Paginator->paginate('Table1');
$this->set(compact('table'));
}

Maximum amount of items in one Page of Yii

I am developing this yii application and in the index i am displaying a "preview" of these items and currently it is display 8 items then it will pageinate itself. I would like make it page every 4 items and i have searched online and found that i could use the CPagination. I have followed the example in the documentation http://www.yiiframework.com/doc/api/1.1/CPagination and it is working but i don't know how to display the model i have the following code in the index.
foreach($models as $model):
endforeach;
$this->widget('CLinkPager', array(
'pages' => $pages,
));
Also the data that i want to display is Title, Content, Image and id and can i use the _view since i have everying set from there regarding css.
Also by defult the yii application display the index with a CListView is there a way where i can set the item limit there instead of using CPagination
If you want to just display 4 items on the index page, edit the index action like this:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('{Model}',
array(
'criteria' => array(
'select' => 'title_field,content_field,image_field',
'limit' => {Limit},
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
This code will return {Limit} models from the database, if you want to sort them by any field you will need to add 'order' to the criteria array lik this:
'criteria' => array(
'select' => 'title_field,content_field,image_field',
'order' => 'download_id DESC',
'limit' => {Limit},
)
In your view, use foreach and render partial to render the _view.
foreach($models as $model):
$this->renderPartial('//{Model}/_view', $model);
endforeach;
Hope that helps.

CakePHP conditional find

I'm writing a simple Blog application. I am dealing with two models, BlogPost and BlogCategory.
They both have a HABTM relationship to each other using a separate table.
I have written a function in the BlogPost model to load all the posts from a certain category, but it keeps producing a database error.
public function getRecentFromCategory($category, $offset, $limit){
return $this->find('all', array(
'order' => 'BlogPost.date desc',
'offset' => $offset,
'limit' => $limit,
'conditions' => array('BlogCategory.id' => $category)
));
}
Why can't I make a conditional based on the associated categories?
You need to set up a belongsTo relationship between your models, meaning your BlogPosts can belong to a BlogCategory.
Assuming your BlogPost has a 'blog_category_id' field, in your BlogPost model:
public $belongsTo = array(
'BlogCategory' => array(
'className' => 'BlogCategory',
'foreignKey' => 'blog_category_id'
)
);
Then your find should be able to filter based on category in the way you want.

Yii Active Record adding multiple conditions in named scopes from different tables

I am trying to make a condition that the client ID must match the client ID and a region field in the locations table must match the region attached to the location id that is associated with the users location.
So I have 3 tables. The current table(t1), the table relation1 refers to (t2), and the table that t2 refers to in relation2 (we will call this t3).
$this->getDbCriteria()->mergeWith(array(
'with' => $rel,
'condition'=>'relation1.client_id=:client_id AND
relation1.relation2.region=:region',
'params'=>array(':client_id'=>$client_id, ':region'=>$region),
));
return $this;
Relation1 is the relation in the table once removed from this one. The table that relation1 refers to has a relation called relation2 that gets me to where I need to retrieve the region value.
If I remove the second condition and parameter it works, so I know relation1 is working, and relation2 also works in other contexts. Here they are if would like to see them.
public function relations()
{
return array(
'relation1' => array(self::BELONGS_TO, 't2', 't2_id'),
);
}
and
public function relations()
{
return array(
'relation2' => array(self::BELONGS_TO, 't3', 't3_id'),
);
}
I really feel like this should work. Any help?
I got the answer from another site. If you are interested, the solution is:
$this->getDbCriteria()->mergeWith(array(
'with' => array(
'relation1' => array(
'condition' => 'relation1.column = :column_id',
'params' => array(':column_id'=>$column_id)),
'relation1.relation2' => array(
'condition' => 'relation2.column2 = :column2',
'params' => array(':column2'=>$column2))
),
));
return $this;
Thanks for your help!

containable on hasMany relationship

Greetings,
I am trying to tear down query returned from find call using containable in CakePHP.
for example I have 2 models, User and Post. User hasMany Post.
Now when I am using containable on find call like so:
$User->id = 1;
$User->find('first', array(
'fields' => array('id'),
'contain' => array('Post')
))
It will not return the associated Post, instead will just return the id of the user.
It works however if I am trying to fetch the data the other way around.
i.e this works:
$Post->find('first', array(
'fields' => array('id', 'user_id'),
'conditions' => array('Post.user_id' => 1),
'contain' => array('User')
))
this doesn't:
$Post->find('first', array(
'fields' => array('id'),
'conditions' => array('Post.user_id' => 1),
'contain' => array('User')
))
From the returned values I then suppose that for the containable to works, the foreignKey has to be in the fields.
How then would I be able to filter out the User fields on the first call as the association of user is stored in Post.user_id?
Any help is greatly appreciated! Thank's.
-aw
As larryb82 said you'll need to define the relationship in both directions in order to retrieve Posts data from the User model
A user has many posts.
A post belongs to an user
CakePHP Doc example

Categories