cakephp-1.3

Paginate results filtered by condition on associated model (HABTM) using Containable

别等时光非礼了梦想. 提交于 2019-12-06 05:40:59
I need to paginate list of Product s belonging to specific Category (HABTM association). In my Product model I have var $actsAs = array('Containable'); var $hasAndBelongsToMany = array( 'Category' => array( 'joinTable' => 'products_categories' ) ); And in ProductsController $this->paginate = array( 'limit' => 20, 'order' => array('Product.name' => 'ASC'), 'contain' => array( 'Category' => array( 'conditions' => array( 'Category.id' => 3 ) ) ) ); $this->set('products', $this->paginate()); However, resulting SQL looks like this: SELECT COUNT(*) AS `count` FROM `products` AS `Product` WHERE 1 = 1

CakePHP: How do I count the number of hasMany records in a find?

荒凉一梦 提交于 2019-12-06 04:44:18
问题 I have two models, Post hasMany Comment . How do I select all Post that have less than two Comment ? I tried using a find with 'fields'=>array('COUNT(Comment.id) as numComments','Post.*') , (and then doing a numComments < 2 in 'conditions' ). But, I get a Unknown column 'Comment.id' in 'field list' error. Thanks! EDIT: I've gotten CakePHP to generate this query: SELECT `Post`.*, FROM `posts` AS `Post` LEFT JOIN comments AS `Comment` ON (`Post`.`id` = `Comment`.`text_request_id`) WHERE COUNT(

jQuery POST to CakePHP $this->data

旧巷老猫 提交于 2019-12-06 02:55:26
I'm trying to use jQuerys post-function to post a form to a CakePHP-script. Like this: jQuery: $('#submit_btn').click(function(){ //Code to prevent redirect dataString = 'test=testdata'; $.post('cakephp/forms/output', dataString, function(response){ alert(response); }) }); CakePHP function output(){ pr($this->data); # Print nothing pr($_POST); # Print test => testdata $this->render('view','ajax'); # Render ajax-friendly } So $_POST isn't empty but $this->data is... how come?? The form element i which to post data from is got from aja, if that is something that matters in this case. $this->data

Calling function in view of cakephp

可紊 提交于 2019-12-05 21:08:51
I have one team array and want that team name every where to show team name.It is possible to built a global function which can return team name and I call that function from my view means ctp file. please try this for west: <?php // controller name like app,users // action name like getdata should be in controller // and you can send parameter also $output = $this->requestAction('controllerName/actionName/'.$parameter); ?> There are multiple approaches to this. What I cannot tell from your description is exactly what you are looking for. If it is simply to create an array of items that is

CakePHP preserving validation errors after redirecting

我们两清 提交于 2019-12-05 20:34:09
I have an element which displays a comment form, the action is comments/add. If the form doesn't validate, I don't want users to go to comments/add (the view for which does not exist), but I want them to remain on the same page and see the validation errors there. However, redirecting to $this->referer() doesn't work - the validation errors disappear and just the flash message remains. public function add(){ if (!empty($this->data)){ $this->Comment->create(); if ($this->Comment->save($this->data)){ $this->Session->setFlash('Comment added.','success'); $this->redirect($this->referer()); }else{

CakePHP Delete Cookie Issue

隐身守侯 提交于 2019-12-05 17:59:27
I've browsed through other people's issues similar to this, but nothing seems to be exactly like what I am experiencing. Please feel free to reference me to another article if this has been addressed before. I have written a cookie when a user authenticates that stores some basic user info locally. When the user logs out, I am trying to delete the cookie variable, but is does not delete. If I use the destroy method, then the cookie is removed, but I am curious as to what I am doing wrong here: Cookie is written like this and is working: function login(){ if($this->Auth->login($this->data)){

how do you create a select with both value and the label the same from an array in cakephp?

巧了我就是萌 提交于 2019-12-05 12:15:10
I created an array in the controller from a table : $tasktemplateResults = $this->Tasktemplate->find('list'); And in the view I have a form helper that creates a drop down list: echo $this->Form->input('Tasktemplate.id', array('options' => array($tasktemplateResults),'label' => 'Task')); The html output is as follows: <div class="input select"> <label for="TasktemplateId">Task</label> <select name="data[Tasktemplate][id]" id="TasktemplateId"> <option value="1">Test task</option> <option value="2">Second test task</option> </select> </div> I would like to have this html output: <div class=

CakePHP GROUP and COUNT items returned in list

五迷三道 提交于 2019-12-05 08:02:38
I know there are some similar quesiton like this on here but they are all about when using Model->find('all'); But thats not what I am doing, I am doing: Model->find('list'); Which is whats making the difference between this working and not working. Given a group of Products I want to find all the brands in that group and how many of each brand. Sounds simple enough, here is what I did: $fields = array('Product.brand','COUNT(`Product`.`brand`) AS brand_count') $brand_data = $this->Product->find('list',array( 'fields'=>$fields, 'conditions'=>$conditions, 'recursive'=>0, 'group' => 'Product

File Upload validation only on record create, not edit

点点圈 提交于 2019-12-05 07:52:31
问题 I've got a form that allows a file upload and I'm using data validation to check what type of file it is and that's all good. What I want to do is then allow them to edit that same record but a file upload at that time is not needed. My validation rule is as follows: 'header_pic' => array( 'extension' => array( 'rule' => array('extension', array('jpeg', 'jpg', 'gif', 'png')), 'message' => 'You must supply a GIF, PNG, or JPG file.', ) ) But this rule requires a file. I found the 'on' parameter

How can I assign an id and class with $this->Html->link(..);?

巧了我就是萌 提交于 2019-12-05 05:58:11
echo $this->Html->link( 'more', array( 'controller'=>'posts', 'action'=>'view', $post['Post']['id'] ) ); How can I assign an id and class for this anchor/link? I want to override its css rule. HTML attributes can be specified in an array as the third parameter. echo $this->Html->link( 'more', array( 'controller'=>'posts', 'action'=>'view', $post['Post']['id'] ), array( 'id' => 'myId', 'class' => 'myClass' ) ); More info in the Cookbook 2.x for the 2.x version or Cookbook 1.3 for CakePHP 1.3. 来源: https://stackoverflow.com/questions/6889955/how-can-i-assign-an-id-and-class-with-this-html-link