CakePHP bake all unknown issue - php

I'm new to Cake php.
I have a problem using bake
I set up a migration with user table
public $migration = array(
'up' => array(
'create_table' => array(
'users' => array(
'user_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
'username' => array('type' => 'string', 'null' => false, 'length' => 250),
'password' => array('type' => 'text', 'null' => false),
'created' => array('type' => 'string', 'null' => false, 'length' => 14),
'modified' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 14),
'indexes' => array(
'PRIMARY' => array('column' => 'user_id', 'unique' => 1)
)
)
)
),
'down' => array(
'drop_table' => array(
'users'
)
)
);
and migrate this file on the db then I tried to do the command "cake bake all"
the problem is that User made a reference to itself with belongsTo and hasMany
is that the default of using bake?
class User extends AppModel {
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'user_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'username' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'date_created' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
}
is there something I missing here?
Should I leave that and modify it manually or there's a config that I missed.

There are three easy solutions, as the last answer stated cake bake all is following default cake conventions and creating associations by the fact that you have a user_id in the table
Either rename the database field from user_id to id and run the shell again.
Run cake bake and manually select model and configure the database associations manually with the shell helper
Manually remove the associations in the model, and the references in the controller and views.

It's correct.
When you use the cake bake all it automatically adds references to your table by convention. As your table refers to herself he identified the 'hasMany' and 'belongsTo' relationships. You should remove the link that doesn't exist or change that is generated by default. Reference: baking custom projects

Related

Add PHP function in CakePHP model array

I have an array of behaviour $actas.Problem is when I adding date() function on the array string,it's return an error:
Example:
public $actas = array(
'Uploader.Attachment'=>array(
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/files/uploads/books' . date('d-m-Y'),//this is where I want to add the function.
'transportDir' => ''
)
)
);
however it's not working. I also do like this:
public $actas = array(
'Uploader.Attachment'=>array(
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => "/var/www/html/apps/webroot/files/uploads/books'".date('d-m-Y')."'",//this is where I want to add the function.
'transportDir' => ''
)
)
);
also not worked.
So my question is how to do that? If I have a lot of mistaken,please tell me so I can learn more about the matter.
Thanks in advance.
This is the full source code of Post.php model
<?php
App::uses('AppModel', 'Model');
/**
* Post Model
*
* #property Tier $Tier
* #property Category $Category
* #property Comment $Comment
*/
class Post extends AppModel {
//var $now = 'CURDATE()';
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'content' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Tier' => array(
'className' => 'Tier',
'foreignKey' => 'tier_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Category' => array(
'className' => 'Category',
'foreignKey' => 'category_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasMany associations
*
* #var array
*/
public $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'post_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
public $actsAs = array(
//'Containable',
'Uploader.Attachment' => array(
// Do not copy all these settings, it's merely an example
'banner' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => "/var/www/html/apps/webroot/img/banners/",
'transportDir' => '',
'finalPath' => '/img/banners/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
),
'feature' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('gif', 'jpg', 'png', 'jpeg'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/img/features/',
'transportDir' => '',
'finalPath' => '/img/features/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
),
'books' => array(
'maxWidth' => 1200,
'maxHeight' => 1200,
'extension' => array('pdf'),
'nameCallback' => '',
'append' => '',
'prepend' => '',
'tempDir' => TMP,
'uploadDir' => '/var/www/html/apps/webroot/files/uploads/books' . date('d-m-Y'),
'transportDir' => '',
'finalPath' => '/files/uploads/books/',
'dbColumn' => '',
'metaColumns' => array(),
'defaultPath' => '',
'overwrite' => true,
'transforms' => array(),
'stopSave' => true,
'allowEmpty' => true,
'transformers' => array(),
'transport' => array(),
'transporters' => array(),
'curl' => array()
)
)
);
}
ahh ok I was not looking at it with my OO hat on. you can't do this because:
Properties
Class member variables are called "properties". You may also see them
referred to using other terms such as "attributes" or "fields", but
for the purposes of this reference we will use "properties". They are
defined by using one of the keywords public, protected, or private,
followed by a normal variable declaration. This declaration may
include an initialization, but this initialization must be a
constant value--that is, it must be able to be evaluated at compile
time and must not depend on run-time information in order to be
evaluated.
you need to use the __construct() method

Cakephp is failing to auto generate record id on insert

I have a very simple join model in cake php. I'm encountering a bug where the ID for each new record inserted is "".
<?php
App::uses('AppModel', 'Model');
/**
* Subscriber Model
*
* #property ChallengeMember $ChallengeMember
* #property Activity $Activity
*/
class Subscriber extends AppModel {
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'ChallengeMember' => array(
'className' => 'ChallengeMember',
'foreignKey' => 'challenge_member_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Activity' => array(
'className' => 'Activity',
'foreignKey' => 'activity_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
This is what the table looks like:
public $fields = array(
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'activity_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 37, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'challenge_member_id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 37, 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1'),
'indexes' => array(
),
'tableParameters' => array('charset' => 'latin1', 'collate' => 'latin1_swedish_ci', 'engine' => 'InnoDB')
);
When testing,
$this->Subscriber->create(array('challenge_member_id' => 'test123456','ChallengeMember.first_time_in_dash' => '1'));
$this->Subscriber->save();
Debugger::dump($this->Subscriber->find('all'));
Outputs
array(
'Subscriber' => array(
'id' => '',
'challenge_member_id' => 'test123456',
'activity_id' => '',
'created' => '2014-07-16 18:45:14'
)
........
As you can see cake is failing too autogen the ID, which is out of character to the rest of my models. Any ideas ? I've re-baked the model and test fixtures but no joy!
Ok, so I never quite got to the bottom of this, the issue only seems to be with the table named "Subscriber". Every other table in has an ID field defined as:
'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'latin1_swedish_ci', 'charset' => 'latin1')
On Model::save(), cakephp auto generates an ID like the one below:
53c4fe0c-c38c-4915-a056-111fd1f54a2f.
In the end I just set the subscriber ID field to "INT(11) Auto Increment", letting mysql take care of key generation. This solved the issue.

CSV Export including row from other table

I am trying to export to a CSV in CakePHP.
Basically the problem that I am having is, the database is split down into multiple tables.
One for a product, one for artists and one for users.
It is all working well apart from the fact that the Email addresses for the artists are stored in the users table, I would like these to be included in the export. I have tried adding in the appropriate header for the column (csv) and then adding the
$result['Users']['username'],
Row that can be seen in my full function below, however it leaves the Email fields blank. Please help!
function export($event_id)
{
$this->layout = 'blank_layout';
ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
//create a file
$filename = "export_".date("Y.m.d").".csv";
$csv_file = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'"');
$results = $this->Sculpture->find('all', array('conditions' => array('event_id' => $event_id), 'order' => 'artist_id'));
// The column headings of your .csv file
$header_row = array(
"Artist",
"Email",
"Sculpture",
"Materials",
"Description",
"Price",
"Notes"
);
fputcsv($csv_file,$header_row,',','"');
// Each iteration of this while loop will be a row in your .csv file where each field corresponds to the heading of the column
foreach($results as $result)
{
// Array indexes correspond to the field names in your db table(s)
$row = array(
$result['Artist']['name'],
$result['Users']['username'],
$result['Sculpture']['title'],
$result['Sculpture']['materials'],
$result['Sculpture']['description'],
$result['Sculpture']['price'],
$result['Sculpture']['notes'],
);
fputcsv($csv_file,$row,',','"');
}
fclose($csv_file);
}
Sculpture Model
<?php
App::uses('AppModel', 'Model');
/**
* Sculpture Model
*
* #property Artist $Artist
* #property Event $Event
*/
class Sculpture extends AppModel {
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'artist_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'event_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Please tell us the name of your sculpture',
'allowEmpty' => true,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'materials' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Please tell us the materials used in this sculpture',
'allowEmpty' => true,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'price' => array(
'money' => array(
'rule' => array('numeric'),
'message' => 'Please include the price of your sculpture.'
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
)
);
public function beforeValidate()
{
//$p = $this->data['Sculpture']['price'];
// Get decimal place if available
//$dec = substr(, $start)
$this->data['Sculpture']['price'] = preg_replace("/[^0-9.]/", '', $this->data['Sculpture']['price']);
return true;
}
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'Artist' => array(
'className' => 'Artist',
'foreignKey' => 'artist_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Event' => array(
'className' => 'Event',
'foreignKey' => 'event_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
/**
* hasAndBelongsToMany associations
*
* #var array
*/
public $hasAndBelongsToMany = array(
'MediaFile' => array(
'className' => 'MediaFile',
'joinTable' => 'sculptures_media_files',
'foreignKey' => 'sculpture_id',
'associationForeignKey' => 'media_file_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'deleteQuery' => '',
'insertQuery' => ''
)
);
}
NEW Error report
$this->loadModel('Artist');
$artist = $this->Artist->find('first', array('conditions' => array('Artist.user_id' => $this->Auth->user('id'))));
$this->set('artist_id', $artist['Artist']['id']);
EventsController::view() - APP/Controller/EventsController.php, line 78
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 92
NEW NEW ERROR From Sculpture Model
$results = $this->Sculpture->find('all', array('conditions' => array('event_id' => $event_id), 'order' => 'artist_id', 'recursive' => 2));
debug($results); die();
The New error
$this->loadModel('Artist');
$artist = $this->Artist->find('first', array('conditions' => array('Artist.user_id' => $this->Auth->user('id'))));
$this->set('artist_id', $artist['Artist']['id']);
EventsController::view() - APP/Controller/EventsController.php, line 78
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 486
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 187
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 162
[main] - APP/webroot/index.php, line 92
<div class="cake-debug-output">
<span><strong>/app/Controller/SculpturesController.php</strong> (line <strong>70</strong>)</span>
<pre class="cake-debug">
array(
(int) 0 => array(
'Sculpture' => array(
'id' => '462',
'artist_id' => '1',
'event_id' => '1',
'title' => '',
'materials' => '',
'description' => '',
'edition' => '',
'price' => '0.00',
'vat' => false,
'media_file_id' => '0',
'delivery' => '2013-12-16',
'notes' => '',
'created' => '2013-12-16 12:52:14',
'modified' => '2013-12-16 12:52:14'
),
'Artist' => array(
'id' => '1',
'name' => 'Amanda Noble',
'website' => 'www.thefusedgarden.co.uk',
'materials' => 'Glass and stainless steel',
'location' => 'Northants',
'created' => '2013-04-30 14:53:25',
'modified' => '2013-07-09 15:21:53',
'user_id' => '11',
'User' => array(
'password' => '*****',
'id' => '11',
'username' => 'noblept#aol.co.uk',
'role_id' => '4',
'created' => '2013-07-01 15:26:40',
'modified' => '2013-07-01 15:26:40'
),
'Sculpture' => array(
(int) 0 => array(
'id' => '138',
'artist_id' => '1',
'event_id' => '2',
'title' => 'Midsummer Blue',
'materials' => 'Glass & Stainless Steel',
'description' => 'One of 3 fused glass panels. sold either as a single panel or as a set of 3',
'edition' => '',
'price' => '144.00',
'vat' => false,
'media_file_id' => '0',
'delivery' => '2013-07-28',
'notes' => 'When sold as a set of three the price is 10% lower.
We will arrive AM, but will not require assistance to install our sculptures',
'created' => '2013-07-09 15:06:56',
'modified' => '2013-07-09 15:21:07'
),
(int) 1 => array(
'id' => '159',
'artist_id' => '1',
'event_id' => '2',
'title' => 'Blue evening',
'materials' => 'Glass and Stainless Seel',
'description' => 'One of 3 fused glass panels sold either as a single panel or as a set of 3 ( the other 2 are Midsummer Blue and Blue Haze )',
'edition' => '',
'price' => '128.00',
'vat' => false,
'media_file_id' => '0',
'delivery' => '2013-07-29',
'notes' => 'Each panel is unique as no piece of fused glass will ever be identical, but it is possible to create a similar, but not exact replica of any panel.
The images shown are of a similar sets, but the actual exhibits are still in the process of creation and an image is not available.',
'created' => '2013-07-09 15:30:53',
'modified' => '2013-07-09 16:31:17'
),
if you are following cake namning convention it should be
$result['User']['username'],
(Model name must be singular and not plural)
edited after seeing Scuplure model file:
Because User is related to Artist Model (and not directly to Sculpture Model) you have to set 'recursive' parameter in your find call:
$results = $this->Sculpture->find(
'all',
array(
'conditions' => array('event_id' => $event_id),
'order' => 'artist_id',
'recursive' => 2
)
);

cakephp: saveAll is not working

The saveAll is not working can some one help me in finding what is the problem.I have included the controller's action, the array passed to saveAll and the model of it.
The controllers action containing the saveAll looks like the following-
public function add_customer_order() {
if ($this->request->is('post')) {
-
-
-
-
$this->CustomerOrderItem->create();
if ($this->CustomerOrderItem->saveAll($customer_order_item)) {
$this->Session->setFlash('The customer order Items has been saved successfully.', 'default/flash_success');
}
else {
$this->Session->setFlash('The customer order Items not been saved successfully.', 'default/flash_success');
}
}
}
The array $customer_order_item looks as follows-
array(
'CustomerOrderItem' => array(
(int) 0 => array(
'id' => '',
'quantity' => '400',
'unit_cost' => '77',
'pending_quantity' => '400',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '0',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '22'
),
(int) 1 => array(
'id' => '',
'quantity' => '200',
'unit_cost' => '77',
'pending_quantity' => '200',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '0',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '25'
),
(int) 2 => array(
'id' => '',
'quantity' => '400',
'unit_cost' => '77',
'pending_quantity' => '400',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '1',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '23'
),
(int) 3 => array(
'id' => '',
'quantity' => '200',
'unit_cost' => '77',
'pending_quantity' => '200',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '1',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '24'
),
(int) 4 => array(
'id' => '',
'quantity' => '200',
'unit_cost' => '77',
'pending_quantity' => '200',
'packaging_configuration_quantity' => '20',
'exercise_duty_id' => '1',
'tax_id' => '5',
'customer_order_id' => '',
'master_article_id' => '27'
)
)
)
The model looks as follows-
<?php
App::uses('AppModel', 'Model');
/**
* CustomerOrderItem Model
*
* #property CustomerOrder $CustomerOrder
* #property MasterArticle $MasterArticle
*/
class CustomerOrderItem extends AppModel {
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'customer_order_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'master_article_id' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* #var array
*/
public $belongsTo = array(
'CustomerOrder' => array(
'className' => 'CustomerOrder',
'foreignKey' => 'customer_order_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'MasterArticle' => array(
'className' => 'MasterArticle',
'foreignKey' => 'master_article_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
I am pretty sure it is a data issue either due to validation or null in db.
debug the contents of debug($this->request->data);
debug the invalid fields if any debug($this->Submission->invalidFields());. Make sure you call this after you have issues validates() request or saveAll()
Try
if ($this->CustomerOrderItem->saveAll($this->request->data)) {
First of all remove all the validation rules and then check again if error is still coming.if not then there must be issue with data validation rules or null value of 'customer_order_id'.

can not filter values within cakephp model

I created a categories model. I also created a project model. The project model belongs to the categories model so when you create a new project, you recieve a category drop down to pick which category you want.
One of the categories is "Root" and I do not want this showing in the drop down list. I created my belongsTo method like so
project.php MODEL
var $belongsTo = array(
'User' => array(
'className' => 'User',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Category' => array(
'className' => 'Category',
'conditions' => array('Category.id '=>'1'),
'fields' => '',
'order' => ''
),
);
For my controller I have scaffolding turned on.
Here is my categories model
Category model
class Category extends AppModel {
var $name = 'Category';
var $displayField = 'name';
var $actsAs = array('Tree');
var $validate = array(
'name' => array(
'alphanumeric' => array(
'rule' => array('alphanumeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'parent_id' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'url' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
var $belongsTo = array(
'ParentCategory' => array(
'className' => 'Category',
'conditions' => '',
'foreignKey' => 'parent_id',
'fields' => '',
'order' => ''
),
);
}
I assume you mean remove Root from the drop down menu cake produces with associations? In which case, try this:
$categories = $this->Category->find('list',
array('conditions' => array('Category.name !=' => 'Root')));
$this->set(compact('categories'));
Use 'conditions' => array('Categories !=' =>'1'),, instead.
Validation is used in the saving of data, not in finding.

Categories