I try to make a JsValidate helper, then I need access to validationDefault wrote on my Models table.php, how I can access?
I try
$model = TableRegistry::get($model);
$rules = $model->validationDefault();
but don't work.
I use CakePHP 3.0 ( PHP 5.5 )
You get the validation object by doing:
$rules = $table->validator('default');
Related
For CakePHP2, I used following function for resetting associations.
public function unbindModelAll($reset = true) {
foreach(array('hasOne','hasMany','belongsTo','hasAndBelongsToMany') as $relation){
$this->unbindModel(array($relation => array_keys($this->$relation)), $reset);
}
}
How can I reset them for CakePHP3?
For CakePHP 2.x you can easily clear bindings using empty contain on the model
$this->Model->contain();
For CakePHP 3.x you can try, I didn't test it:
$this->Model->find()->contain();
I am very new in cakephp and i have to upgrade a cake project from version 1.1 to 3.6. I do not know how to convert these lines of code to cakephp 3.6:
App::import('Model', 'SystemMenu');
$system_menu =& new SystemMenu();
SystemMenu is an model which was define in Model folder.
Thank you very much for your help.
If youre within a controller, you can do
$this->loadModel('SystemMenus');
and access the model like so
$this->SystemMenus->find()->...
If not, you can use TableRegistry
$systemMenus = TableRegistry::get('SystemMenus')
And access is simple:
$systemMenus->find()->...
See https://book.cakephp.org/3.0/en/orm/table-objects.html for more information
Notice that i have changed the table name to be plural, as the CakePHP 3.x conventions specifies https://book.cakephp.org/3.0/en/intro/conventions.html
You can use TableRegistry class.
$system_menu = \Cake\ORM\TableRegistry::get('SystemMenu');
//new entity
$entity = $system_menu->newEntity();
//get entity by id
$entity = $system_menu->get(2);
//Save entity
$system_menu->save($e);
// finder
$menu = $system_menu->find()->toArray();
Trying to get ID by findBySlug method in CakePHP, for example
$this->Page->findBySlug(1);
this will return all fields, but i need only ID to be returned , i have searched lot but could not find function reference as well and try following but get an Error
$this->Page->findBySlug(15, array('fields'=>array('Page.id')));
any solution within the function that doesn't use a custom query?
cakephp2
$page = $this->Page->findBySlug(1, array('id'));
$id = $page['Page']['id'];
cakephp3
$page = $this->Pages->findBySlug(1)
->select('id');
$id = $page->id;
I am trying to run a query in AppController on a table that has no Model associated with it. I don't want to use a Model cause this query would fire on every request and I guess using a Model would make it a bit slower.
I found out in one forum that this can be achieved with the following code in CakePHP 1.3
$db = ConnectionManager::getInstance();
$conn = $db->getDataSource('default');
$conn->rawQuery($some_sql);
But this is not working in CakePHP 2.1.3.
Any help would be appreciated.
Thanks :)
The getDataSource() method is static in CakePHP 2.x, so you should be able to use:
$db = ConnectionManager::getDataSource('default');
$db->rawQuery($some_sql);
you should run this way
App::uses('ConnectionManager', 'Model');
$db = ConnectionManager::getDataSource('default');
if (!$db->isConnected()) {
$this->Session->setFlash(__('Could not connect to database.'), 'default', array('class' => 'error'));
} else {
$db->rawQuery($some_sql);
}
rawQuery will not return data, use $db->query instead.
$db = ConnectionManager::getDataSource('default');
$data = $db->query($some_sql);
I searched a lot but I couldn't find on How to use the find('all') in Views as used in Rails, but here I'm getting the error "Undefined property: View::$Menu [APP\Lib\Cake\View\View.php, line 804]"
'Menu' is the model which I'm using to fetch data from the menus table.
I'm using the below code in views:
$this->set('test',$this->Menu->find('all'));
print_r($test);
Inside your Menu model create a method, something like getMenu(). In this method do your find() and get the results you want. Modify the results as you need and like to within the getMenu() method and return the data.
If you need that menu on every page in AppController::beforeFilter() or beforeRender() simply do
$this->set('menu', ClassRegistry::init('Menu')->getMenu());
If you do not need it everywhere you might go better with using requestAction getting the data using this method from the Menus controller that will call getMenu() from the model and return the data. Setting it where you need it would be still better, if you use requestAction you also want to cache it very likely.
TRY TO NOT RETRIEVE DATA WITHIN VIEW FILE. VIOLATION OF MVC RULE
try this in view file:
$menu = ClassRegistry::init('Menu');
pr($menu->find('all'));
In AppHelper ,
Make a below function
function getMenu()
{
App::import('Model', 'Menu');
$this->Menu= &new Menu();
$test = array();
$test = $this->Menu->find('all');
return $test;
}
Use above function in view like :
<?php
$menu = $html->getMenu();
print_r($menu);
?>
Cakephp not allow this .
First create the reference(object) of your model using ClassRegistry::init('Model');
And then call find function from using object
$obj = ClassRegistry::init('Menu');
$test = $obj->find('all');
echo ""; print_r($test); `
This will work.