CakePHP loadModel not working - cakephp

I have created a Model named "Usermgmt" and I am using table for this model is "users" in following manner.
public $useTable = "users";
Now I am loading this Model into different controller named "MembersController" in following manner
public $this->loadModel('Usermgmt');
But it is showing error when I load this model into "MembersController" controller- table "usermgmts" missing for "Usermgmt" model.
And it is working fine in "UsermgmtController" without showing table "usermgmts" missing error and saving data correctly into the "Users" table.
NOTE- I do not have any table with named - "usermgmts" as I am using "users" table for "Usermgmt" Model

Remove the public portion and call it from inside a function:
So if you are trying to add this is a function in MembersController, you would the add it like this:
App::uses('AppController', 'Controller');
class MembersController extends AppController {
public function myFunction() {
$this->loadModel('Usermgmt');
//do stuff
}
}

Related

how to implement beforeDelete() event in cakephp 3 for all models

In cakephp 3 we can define beforDelete event listener in any Model. But how to use this event for all models. I want to detect all cascade records conditions before delete one record in all exists models.
namespace App\Model\Table;
use Cake\ORM\Table;
class ArticlesTable extends Table{
public function initialize(array $config)
{
$this->primaryKey('my_id');
}
public function beforeDelete(Event $event, EntityInterface $entity,ArrayObject $options)
{
return false;
}
}
how to use this code for all models. should be this code in appcontroller?
This is pretty easy by using the event system. Read the whole chapter to understand the events first.
Create an event listener that listens to this event
Do whatever you want in the listeners callback method, add your logic there
Attach the listener to the global event manager in your bootstrap.php
I usually create behavior class and add the functionality there which will be shared by most of the Table objects. I don't know it's better approach or not but here are the steps i follow.
First create behavior class with bake command bin/cake bake behavior , this will create properly namespaced class , and add beforeDelete method there.
Include use ArrayObject; use Cake\Event\Event; use Cake\ORM\Entity;at the top
if bake command hasn't added already.
public function beforeDelete(Event $event, Entity $entity, ArrayObject $options){
//your code goes here
// $this->_table is Table object instance behavior is attached to
}
Now attach behaviour to your Table classes
class ArticlesTable extends Table{
public function initialize(array $config)
{
$this->addBehavior('YourBehaviorNeme');
}
}
For more info see http://book.cakephp.org/3.0/en/orm/behaviors.html
Another solution that will work for all models is to:
Create class MyTable extends Table
Define beforeDelete method in that class
Make all other table classes extend MyTable

CakePHP update public $uses = false; in action from same Controller

I'm writing an installation script for my CakePHP web application. I have a InstallController with 6 actions: step1, step2, step3, etc.
At step1 I'm handling Config/database.php creation. Because this file is empty and no datasource is available I have to set public $uses = false; in the InstallController.
At step2 the Config/database.php file is set so I should be able to make a connection to the datasource. This is also necessary because I want to update some database fields in the following steps.
Is it possible to update the public $uses = false; in every following steps after step1?
I'm using CakePHP version 2.3.5
Have you considered loading the model within the actions? So, something like:
<?php
App::uses('AppController', 'Controller');
class InstallController extends AppController {
public $uses = false;
public function step1() {
}
public function step2() {
$this->loadModel("Install");
$this->Install->callMethod();
}
}
In CakePHP 2.x models are lazy loaded, so as long as your step1 action doesn't try to make use of a model, you can safely declare the models in your controllers $uses property, they are not being constructed until your code actually makes use of them.
However, if for some reason you'd actually need to modify $uses, well then just do it, as mentioned models are lazy loaded, so you can modify $uses whenever you want and then access the models afterwards via magic properties on the controller.

cakephp changes when changing the name of the database table

I work in cakephp, I have a database table named 'movements' I want the named 'movements_bags' what changes need to be made​​:
- The model
-the controller
-folder view
To use a table with a name that is outside the CakePHP table naming convention (e.g. Movement model has table names movements, but you want to use the table named movements_bags) you simply specify which table name to use in your model with the $useTable property:
class Movement extends AppModel {
var $useTable = 'movements_bags'; // default would be movements
// ...
}
I prefer Scrowler answer, but if you are not happy with this answer you can try bellow code.
change controller name to MovementsBagsController.
And change controller class name
class MovementsBagsController extends AppController {
}
Change your model name to MovementsBag
and change model class name
class MovementsBag extends AppModel {
}
change view folder to MovementsBags
I think that will work fine now.

Laravel Relationships (user-upload)

I get the following errors when i am trying to save the data with the file upload function because I have declared a new uploader_id to my table which should get its value from the relationship I wrote in the model.
Illuminate \ Database \ QueryException
SQLSTATE[HY000]: General error: 1364 Field 'uploader_id' doesn't have a default value (SQL: insert into `assets` (`filename`, `title`, `body`, `filepath`, `extension`, `realpath`, `mime`, `updated_at`, `created_at`) values (users.mov, New, dummy text data. , /uploads/User Test/2014-03-04_00:48, mov, /private/var/tmp/phpHDpmEI, video/quicktime, 2014-03-04 00:48:59, 2014-03-04 00:48:59))
In my models I defined the following
User Model: where assets its the table name for the uploads table in database
public function assets() {
return hasMany('assets');
}
Upload Model:
public function user() {
return belongsTo('user', 'uploader_id');
}
I have tried in my controller where i am using Upload::create(array()) function to post the data to the database, to add another parameter called 'uploader_id' => Auth::user()->id; but none of this work.
What is the best way to pass the uploader_id to the form each time the user uploads a file ?
That's what events are for:
class Asset extends Eloquent {
public static function boot()
{
static::creating(function($model)
{
$model->uploader_id = Auth::user()->id;
});
}
}
Also your relationships should reference their class -
return $this->hasMany('Asset');

CakePHP doesn't load model

I'm new to cakePhp development. I've stuck on following problem:
I've made few models, controllers and views - it works great. The problem is that after production, I have to made new table(Transactionaltemp table and corresponding model and controller ) in the db that logically is "connected" to other tables, but technically does not needs to - for ex. it holds temporary info on user_id, time, ip and similar. So, other tables doesn't need to be directly connected to that.
The problem is when I try (in some other controller than transactionaltemps_controller):
$this->loadModel('Transactionaltemp');
I get error - the model is not found (it is true because the model is missing in the cache). Interesting enough transactionaltempls_controller is in the cache (in the cake_controllers_list file).
I tried following stuff to resolve the problem:
clear cache
disable cache
tried using uses={..} code in the controller that I would like to use mymodels_controller
tried using init('Transactionaltemp')
with no success. Here is corresponding code:
The model:
<?php
class Transactionaltemp extends AppModel
{
var $name = 'Transactionaltemp';
function beforeSave() {
return true;
}
}
?>
The controller:
<?php
class TransactionaltempsController extends AppController
{
var $name = 'Transactionaltemps';
var $scaffold;
}
?>
I'll very grateful to any help!!!
App:Import('Model','Transactionaltemp');
$this->Transactionaltemp= new Transactionaltemp;
I hope this may work
If you are connecting to a table name with different name than your model, you must specify the table name in it:
<?php
class Transactionaltemp extends AppModel
{
var $uses = 'Transactional';
var $name = 'Transactionaltemp';
function beforeSave() {
return true;
}
}
Try
App::Import('Model', 'YourModelName');
in your controller (or where you want).

Resources