Never display some records in CakePHP - cakephp

I would like return some records from my base (eg. users roles)
And I use usually function find(), findAll(), etc., and I always must write 'conditions' with like this: not display role admin (name!=admin).
My question is, how I can in RoleModel set for all function will be return with this conditions.
Sorry for english!
Bye!

Use the beforeFind() (http://book.cakephp.org/view/680/beforeFind) callback for this kind of thing. Here's one I use from time to time that ensures only active records are returned:
function beforeFind( $queryData )
{
$conditions = $queryData['conditions'];
if( !is_array( $conditions ) ) {
if( !$conditions ) {
$conditions = array();
}
else {
$conditions = array( $conditions );
}
}
if( !array_key_exists( $conditions, 'active' ) && !isset( $conditions[$this->alias . '.active'] ) ) {
$conditions[$this->alias . '.active'] = 1;
}
return true;
}
That's a bit off the cuff, so the syntax may not be exact, but it should give you something to start with. I think everything's in order except, perhaps, the argument order in a few function calls. Anyway, it should be close.

I think a better solution would be setting the condition in your hasMany relationship.
// User.php Model:
var $hasMany = array('Role' => array('conditions' => array('name <>' => admin)));
and vice versa, you can do it for your Role model:
// Role.php Model:
var $belongsTo = array('User' => array('conditions' => array('User.name <>' => admin)));

Related

CakePHP saveAll repeated and non-repeated entries

Well,
I created a Model with the following restriction
public $validate = array(
'player_id' => array(
'rule' => array(
'checkUnique',
array(
'player_id',
'game_id'
),
true
),
'required' => true,
'allowEmpty' => false,
'on' => 'create',
'message' => 'Same player_id y game_id'
)
);
So each time I try to create a game record in the table it is created only if it is not created yet.
So I created an action in one controller that get recent games of one player and use saveAll to save into the database.
If the database is empty there is no a single problem, of course. But if I receive some games and some of them are already being inserted previously saveAll fails because SOME of the games are already into the database.
public function getRecentGames($server = null, $player = null){
$this->autoRender = false;
if( !empty($server) && !empty($player) ){
$r = $this->_getRecentGames($server, $player, $gamesData);
if ($r['code'] == 200) {
if ($this->Game->saveAll($gamesData, array('deep' => true))) {
pr($gamesData);
prd('Saved');
} else {
pr($this->Game->invalidFields());
prd('Not saved');
}
} else {
}
}
return print_r($gamesData, true);
}
Basically saveAll(..) calls internally validateMany(..) which returns false because not every entry is valid and saveAll does not try to save. This is the normal behavior of CakePHP and the way developers want it to work.
So, what should I do?
Check each game and try to save it?
foreach ($games as $game) {
$this->Model->saveAssociated(..);
}
Modify the behavior of saveAll(..) in order to save the valid games and not the invalid ones. (Do you think this should be the default behavior of CakePHP?)
Other solutions I didn't think(?). Please show me then
Thank you
Well this is the best approach I could think of:
$validations = $this->Game->validateMany( $gamesData, array('deep' => true, 'atomic' => false) );
for ($i=count($gamesData)-1; $i>=0; $i--) {
if (!$validations[$i]) {
unset($gamesData[$i]);
}
}
if (!empty($gamesData)) {
$result = $this->Game->saveAll($gamesData, array('deep' => true, 'validate' => false));
}

Match data entered with data in the database CAKEPHP

I am stuck at the loop function of cakephp.
The logic is I need to compare the data entered by users with the data already in a table. I have two tables, one is Bookings and one is Inventories_Bookings. Below is my coding but it doesnot work. any help! Thanks
public function add2() {
if ($this->request->is('post')) {
foreach ($invbook as $invenbook)
{
if ($this->request->data['Booking']['bookings_location'] == $invenbook['InventoriesBooking']['test'])
{
$this->Session->setFlash(__('The booking cannot be created'));
$this->redirect(array('action' => 'add2'));
debug($this->request->data['Booking']['bookings_location'] == $invenbook['InventoriesBooking']['test']);
}
}
$this->Booking->create();
$invbook = $this->Booking->InventoriesBooking->find('list',array('fields' => array('InventoriesBooking.id', 'InventoriesBooking.test')));
$this->set(compact('invbook'));
}
}
I would use a custom validation function for this.
You are able to create your own functions in the model, and from here you can access the database to do the lookup. If it matches you can return true.
You can read about custom validation methods in the book.
There is an example of a rule like this using the db in the book.
Quoted for great justice.
class User extends AppModel {
public $validate = array(
'promotion_code' => array(
'rule' => array('limitDuplicates', 25),
'message' => 'This code has been used too many times.'
)
);
public function limitDuplicates($check, $limit) {
// $check will have value: array('promotion_code' => 'some-value')
// $limit will have value: 25
$existing_promo_count = $this->find('count', array(
'conditions' => $check,
'recursive' => -1
));
return $existing_promo_count < $limit;
}
}

CakePHP Pagination: how can I sort by multiple columns to achieve "sticky" functionality?

I see that this paginate can't sort two columns at the same time ticket is still open, which leads me to believe that what I'm trying to do is not possible without a workaround. So I guess what I'm looking for is a workaround.
I'm trying to do what many message boards do: have a "sticky" function. I'd like to make it so that no matter which table header link the user clicks on to sort, my model's "sticky" field is always the first thing sorted, followed by whatever column the user clicked on. I know that you can set $this->paginate['Model']['order'] to whatever you want, so you could hack it to put the "sticky" field first and the user's chosen column second. The problem with this method is that pagination doesn't behave properly after you do it. The table header links don't work right and switching pages doesn't work right either. Is there some other workaround?
User ten1 on the CakePHP IRC channel helped me find the solution. I told him that if he posted the answer here then I would mark it as the correct one, but he said I should do it myself since he doesn't have a Stack Overflow account yet.
The trick is to inject the "sticky" field into the query's "order" setting using the model's "beforeFind" callback method, like this:
public function beforeFind($queryData) {
$sticky = array('Model.sticky' => 'DESC');
if (is_array($queryData['order'][0])) {
$queryData['order'][0] = $sticky + $queryData['order'][0];
}
else {
$queryData['order'][0] = $sticky;
}
return $queryData;
}
What you can do is code it in the action. Just create the query you want when some parameters exist on the URL. (parameters has to be sent by GET)
For example:
public function posts(){
$optional= array();
if(!empty($this->params->query['status'])){
if(strlower($this->params->query['status']=='des')){
$optional= array('Post.status DESC');
}
else if(strlower($this->params->query['status']=='asc')){
$optional= array('Post.status ASC');
}
}
if(!empty($this->params->query['department'])){
//same...
}
//order first by the sticky field and then by the optional parameters.
$order = array('Post.stickyField DESC') + $optional;
$this->paginate = array(
'conditions' => $conditions,
'order' => $order,
'paramType' => 'querystring',
);
$this->set('posts', $this->paginate('Post'));
}
I have used something similar to filter some data using $conditions instead of $order and it works well.
You can use custom field for sorting and update pagination component.
Controller code
$order['Document.DATE'] = 'asc';
$this->paginate = array(
"conditions"=> $conditions ,
"order" => $order ,
"limit" => 10,
**"sortcustom" => array('field' =>'Document.DATE' , 'direction' =>'desc'),**
);
Changes in pagination component.
public function validateSort($object, $options, $whitelist = array()) {
if (isset($options['sort'])) {
$direction = null;
if (isset($options['direction'])) {
$direction = strtolower($options['direction']);
}
if ($direction != 'asc' && $direction != 'desc') {
$direction = 'asc';
}
$options['order'] = array($options['sort'] => $direction);
}
if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) {
$field = key($options['order']);
if (!in_array($field, $whitelist)) {
$options['order'] = null;
}
}
if (!empty($options['order']) && is_array($options['order'])) {
$order = array();
foreach ($options['order'] as $key => $value) {
$field = $key;
$alias = $object->alias;
if (strpos($key, '.') !== false) {
list($alias, $field) = explode('.', $key);
}
if ($object->hasField($field)) {
$order[$alias . '.' . $field] = $value;
} elseif ($object->hasField($key, true)) {
$order[$field] = $value;
} elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) {
$order[$alias . '.' . $field] = $value;
}
}
**if(count($options['sortcustom']) > 0 )
{
$order[$options['sortcustom']['field']] = $options['sortcustom']['direction'];
}**
$options['order'] = $order;
}
return $options;
}
Easy insert 'paramType' => 'querystring',
Show Code Example:
$this->paginate = array(
'conditions' => $conditions,
'order' => array(
'Post.name' => 'ASC',
'Post.created' => 'DESC',
),
'paramType' => 'querystring',
);
$this->set('posts', $this->paginate('Post'));

CakePHP find conditions not working

I have an action for CommentsView in which i want to retrieve all the comments with a condition that Comment.post_id = Post.id but when i debug it, it gives me an empty array.
Action CommentsView:
public function commentsview()
{
$commentsview = $this->Comment->find('all', array('conditions'=>array('Comment.post_id' => 'Post.id')));
if (!empty($this->params['requested']))
{
return $commentsview;
}
}
You are providing the condition for a join which is passed differently.
The conditions arguments are for a WHERE clause.
But you only need to specify:
$comments = $this->Comment->find('all',
array(
'conditions'=>array(
'Comment.post_id' => $post_id
)
)
);
Or when you are fetching the comments from the PostsController
$comments = $this->Post->Comment->find('all',
array(
'fields'=>array(
'Comment.*'
)
'conditions'=>array(
'Post.id' => $post_id
)
)
);
change your function to this:
public function commentsview($post_id=null) {
$commentsview = $this->Comment->find('all', array('conditions'=>
array('Comment.post_id' => $post_id))
);
debug($commentsview);
exit;
}
Visit this URL: yourapp.com/comments/commentsview/37
The comments will be output. Now you know it's working. Then you can pass it to the view or do whatever.
You have asked similar questions several times. This is a BASIC concept.

how to tell cakephp to use function index($type) to just go to the index page?

I have this application that directs users to Types of attractions with this function:
public function index($type=null) {
$this->set('title','What to do when you visit Gulf Shores');
$this->paginate['Attraction']=array(
'limit'=>9,
'order'=>array('Attraction.id'=>'asc'),
'conditions'=>array(
'active'=>1,
'attr_type'=>$type
)
);
$c=$this->paginate('Attraction');
$this->set('attractions', $c);
}
and it works great, but I'd like users to also be able to go to a front page /attractions/ that doesn't filter out by attr_type. This function shows zero results (as obviously $type still = null) for the front page. Is there a step I'm missing or must I have a view.ctp file and function in my controller?
You could use an if statement to determine the conditions:
public function index($type = null) {
$this->set('title', 'What to do when you visit Gulf Shores');
$conditions = array(); //create $conditions outside of the if statement
if ($type) { //if $type is equal to anything other than null or 0
$conditions = array(
'active' => 1,
'attr_type' => $type
);
} else {
$conditions = array(
'active' => 1
);
}
$this->paginate['Attraction'] = array(
'limit' => 9,
'order' => array('Attraction.id' => 'asc'),
'conditions' => $conditions
);
$c = $this->paginate('Attraction');
$this->set('attractions', $c);
}
It's not actually necessary to create $conditions outside of the if statement in PHP but it is in a lot of other programming languages because of scope.
If you create a variable inside a if statement is it available outside the if statement?

Resources