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();
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 use JDatabaseDriver for interaction with database.
The next code I took from official Joomla Documentation.
Documentation
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$query
->select($db->quoteName(array('user_id', 'profile_key', 'profile_value', 'ordering')))
->from($db->quoteName('#__user_profiles'))
->setLimit('10');
Look at the last row in code above.
The setlimit method is no exist in JDatabaseDriver class but it is declared in class - JDatabaseQueryMysqli.
Don't I understand the logic or there is some mistake?
In Latest Joomla 3.8.1 setLIMIT does not work properly. You may go for alternative methods like
$query->setQuery($query,start,offset);
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');
I have the following model class:
class Property extends AppModel
{
var $name = 'Property';
var $hasMany = array(
'Inventory' => array(
'className' => 'Inventory',
'foreignKey' => 'property_id'
)
);
}
My database schema is set so that the id fields are all set to CHAR(36) so that CakePHP generates UUIDs for each entity. When I attempt to perform a find on my Property entity, it doesn't seem to be adding the necessary join to retrieve any related Inventories. Does anyone have any experience with this issue?
Thanks!
UUIDs will have nothing to do with it, I have a very similar model setup and use UUIDs everywhere.
You technically don't need those className and foreignKey declarations in there, as you seem to be following the CakePHP convention :)
I'd remove those lines, check your database for actual inventories with product IDs. If not, post the find()s you're doing.
The answer turns out to be somewhat stupid.
I'd migrated this from a CakePHP 2.0 install to a CakePHP 1.3 install (couldn't update PHP on the server I was using), and all of my model classes still had uppercase first characters. Changing the file from Property.php to property.php fixed it.
How can I change the extension for CakePHP Views from .ctp to .php
I have seen there is this line in /cake/libs/view.php var $ext = '.ctp'; that sets the extension but how can I do it from my /app/ folder so it doesn't effect Cake core files.
Thanks
You can set the extension in your AppController with
public $ext = '.yourext';
This is is reply to Cameron's comment regarding the issue of using multiple extensions in light of the fact cakephp does not allow you to specify multiple extensions.
I am using Mustache for a single site that uses merb, rails2, rails3 and cakephp for different sections of the site. The cake site "receives" mustache files for common layout elements but these templates have a '.mustache' file extension which my cake site will not recognize. My workaround is basically what dhofstet suggests just framed in the context of your specific usecase. In short, create a wrapper that might look something like this:
<?
$tmp = $this->ext;
$this->ext = '.mustache';
?>
<?= $m->render($this->renderElement('moznav/advanced_header'), array('foo' => $bar)) ?><br />
<? $this->ext = $tmp; ?>
When flow returns to the caller, you keep on using your native file extension.
How can I change the extension for CakePHP Views from .ctp to .php
I have seen there is this line in /cake/libs/view.php var $ext =
'.ctp'; that sets the extension but how can I do it from my /app/
folder so it doesn't effect Cake core files.
example:
you have view posts/add.ctp
now you rename add.ctp into add.php
and then you run .../posts/add the message error show:
Error: The view for PostsController::add() was not found.
to your app can understand extention .php, you add line public $ext = '.php' in PostsController.php
now, you run again ..posts/add => okie, cakephp understand extention .php
Notice: if you use atrribute $ext = '.php' but file view named .ctp, cakephp extention .ctp will use by default
I found this post because I had the same problem. This is not mentioned in the Predominant TwigView plugin documentation on Github. I'm tired of those documentations that explains only half of things and with which we have to guess the second half. This is a big waste of time that slows down projects pointlessly.