I'm having a very strange issue. I'm working with XAMPP on my local computer, and everything works perfect. But when I upload it to my server, suddenly ContainableBehavior stops recognizing associations.
Cake's version: 2.5.6
Here's XAMPP's phpinfo(): http://pastebin.com/DeZWMh42
And here's my server's phpinfo(): http://pastebin.com/rtZ0kTAM
Both locations have the exact same files and databases.
This is the error(s) I'm getting:
Warning (512): Model "Certificado" is not associated with model
"Usuario" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]
Warning (512): Model "Certificado" is not associated with model
"Alumno" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]
Warning (512): Model "Certificado" is not associated with model
"Usuario" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]
Warning (512): Model "Certificado" is not associated with model
"Alumno" [CORE/Cake/Model/Behavior/ContainableBehavior.php, line 342]
Basically, an Impresion belongs to an Usuario and to a Certificado, the latter also belongs to an Usuario (may be a different one) and to an Alumno. Obviously I cut out all the irrelevant parts (let me know if you need more.)
This is where I'm using ContainableBehavior (Impresion's Controller):
(I'm getting the error on /impresions/index)
class ImpresionsController extends AppController {
public $components = array('Paginator');
public $uses = array('Usuario', 'Alumno', 'Certificado', 'Impresion');
public function index(){
$this->paginate = array(
'limit' => 10,
'order' => array('fecha_creacion' => 'desc'),
'contain'=>array(
'Usuario',
'Certificado' => array(
'Usuario',
'Alumno'
)
),
);
$results = $this->paginate('Impresion');
$this->set('impresiones',$results);
}
}
And in the view I just use a foreach($impresiones).
Impresion's Model:
class Impresion extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'Certificado' => array(
'className' => 'certificado',
'foreignKey' => 'certificado_id',
),
'Usuario' => array(
'className' => 'Usuario',
'foreignKey' => 'usuario_id',
'fields' => array('nombre','codigo')
),
);
}
Usuario's Model:
class Usuario extends AppModel {
public $hasMany = array('Certificado','Impresion');
public $actsAs = array('Containable');
}
Certificado's Model:
class Certificado extends AppModel {
public $actsAs = array('Containable');
public $belongsTo = array(
'Usuario' => array(
'className' => 'Usuario',
'foreignKey' => 'usuario_id',
),
'Alumno' => array(
'className' => 'Alumno',
'foreignKey' => 'alumno_id',
)
);
public $hasMany = 'Impresion';
}
Alumno's Model:
class Alumno extends AppModel {
public $actsAs = array('Containable');
public $hasMany = 'Certificado';
}
Local query: http://pastebin.com/B5MRp3FS
Server's query: http://pastebin.com/J2H4U6Ge
I'm completely lost here. Why does it work perfectly fine on my computer, but breaks on the server? Everything else works, it's just ContainableBehavior that's having problems.
I can't believe it.
Disregard everything.
This:
'className' => 'certificado',
Should've been this:
'className' => 'Certificado',
Sorry for wasting your time, I'm an idiot
Related
I'm having some trouble with CakePHP's models.
I have three tables - CC_PLAYLIST, CC_PLAYLISTCONTENTS, and CC_FILES.
I'd like to get a list of all playlists, with all of their contents, and then the relevant file information for each piece of playlist content.
I.E
Playlist 1
-- Playlist Content 1
---- File Info
-- Playlist Content 2
---- File Info
-- Playlist Content 3
---- File Info
Playlist 2
-- Playlist Content 123
---- File Info
In my controller I have:
$playlistcontent = $this->AirtimePlaylist->find('all') however that seems to turn up a flat array with all playlistcontents, all files, and empty playlist arrays.
AirtimeFile.php
class AirtimeFile extends AppModel {
public $useTable = 'cc_files';
public $actsAs = array('Containable');
public $hasMany = array('AirtimeFileAttribute' => array(
'className' => 'AirtimeFileAttribute',
'foreignKey' => 'track_id'
));
}
AirtimePlaylist.php
class AirtimePlaylist extends AppModel {
public $useTable = 'cc_playlist';
public $actsAs = array('Containable');
public $hasMany = array('AirtimePlaylistContent' => array(
'className' => 'AirtimePlaylistContent',
'foreignKey' => 'playlist_id'
));
}
AirtimePlaylistContent.php
class AirtimePlaylistContent extends AppModel {
public $useTable = 'cc_playlistcontents';
public $actsAs = array('Containable');
public $hasOne = array('AirtimePlaylist' => array(
'className' => 'AirtimePlaylist',
'foreignKey' => 'id'
),'AirtimeFile' => array(
'className' => 'AirtimeFile',
'foreignKey' => 'id'
));
}
In your controller you need to use contain
$model = $this->AirtimePlaylist->find('all',
array(
'contain' => array('AirtimePlaylistContent' => array('AirtimeFile') )
)
);
I try to say just CkMmUsersKta.user_id=CkSetupUser.id and define relationship between 2 tables in CakePhp.
I defined models and called models in function in controller but says Undefined index for
$c['CkSetupUser']['user_name']
CakePHP's
Notice (8): Undefined index: CkSetupUser [APP\Controller\RealsController.php, line 67]
my controller:
class RealsController extends AppController {
public $helpers = array('Html', 'Form');
public $uses =array('CkMmUsersKta','CkSetupUser');
public function index(){
$this>loadModel('CkMmUsersKta');$this>loadModel('CkSetupUser');
ini_set('memory_limit', '2000M');
$cs=$this->CkMmUsersKta->find("all",array('fields'=>array('CkMmUsersKta.id')));
foreach($cs as $c):
echo $c['CkMmUsersKta']['id'].$c['CkSetupUser']['user_name']."<br />";
endforeach;
set_time_limit(0);
}
}
my CkSetupUser model:
public $hasMany = array(
'CkMmUsersKta' => array(
'className' => 'CkMmUsersKta',
'foreignKey' => 'user_id',
'dependent' => true
)
);
my CkMmUsersKta model:
public $belongsTo = array(
'CkSetupUser' => array(
'className' => 'CkSetupUser',
'foreignKey' => 'user_id'
)
);
The problem is that you are fetching only id field in:
$cs=$this->CkMmUsersKta->find("all",array('fields'=>array('CkMmUsersKta.id')));
You should specify the name field also.
$cs=$this->CkMmUsersKta->find("all",array('fields'=>array('CkMmUsersKta.id','CkSetupUser.user_name')));
My system has users with different role. I want show all users in same page in separate tables. So i did following things.
User Model (User.php)
App::uses('AppModel', 'Model');
class User extends AppModel {
public $useTable = 'users';
}
AUser model (Auser.php)
App::uses('User', 'Model');
class Auser extends User {
}
BUser model (Buser.php)
App::uses('User', 'Model');
class Buser extends User {
}
UsersController.php
App::uses('Auser', 'Model');
App::uses('Buser', 'Model');
class UsersController extends AppController {
public function index() {
$Auser = new Auser();
$Buser = new Buser();
$this->paginate = array(
'Auser' => array(
'conditions' => array('Auser.role' => 0),
'limit' => 5
),
'Buser' => array(
'conditions' => array('Buser.role' => 1),
'limit' => 5
),
);
$this->set('ausers', $this->paginate('Auser'));
$this->set('busers', $this->paginate('Buser'));
}
}
But it show following error.
Error: An Internal Error Has Occurred.
What is the issue?
error comma and how to load models is so
$this->loadModel('Auser');
so:
class UsersController extends AppController {
public function index() {
$this->loadModel('Auser');
$this->loadModel('Buser');
$this->paginate = array(
'Auser' => array(
'conditions' => array('Auser.role' => 0),
'limit' => 5
),
'Buser' => array(
'conditions' => array('Buser.role' => 1),
'limit' => 5
)
);
$this->set('ausers', $this->paginate('Auser'));
$this->set('busers', $this->paginate('Buser'));
}
}
Sorry if the Title misses what i'm aiming at, but had no clue how to name this. Here's my Quest:
I have a single Cart, holding multiple Items (1:n) which are belonging to prices (1:1).
The Relations between Items and Prices are working as well:
class Item extends AppModel {
public $hasOne = 'Price';
}
class Price extends AppModel {
public $belongsTo = array(
'Item' => array(
'className' => 'Item',
'foreignKey' => 'id'
)
);
}
But now i make a select (find) of a cart and want those items included, which is also working as well:
class CartItem extends AppModel {
public $useTable = 'cart_items';
public $belongsTo = array('Item', 'Address');
}
But what i really need is to get those prices of each item, which is not working ($result in afterFind()-Callback in Item-Model does not include the assigned prices & afterFind()-Callback in Price-Model is not called when finding a cart)..
what am i missing here?
/EDIT: Recent Changes:
class AppModel extends Model {
var $actsAs = array('Containable');
}
class CartsController extends AppController {
public function getCart() {
$cart = ClassRegistry::init('CartItem')->find('all', array(
'contain' => array(
'Item' => array(
'Price'
),
'Address'
),
'conditions' => array(
'id_cart' => $cart['Cart']['id']
)
));
}
The above changes cause that i'll get a Price into a found Item but only into the last one thats found.
You haven't shown your actual find(), but I suspect you're not setting an appropriate 'recursive' param or are not using 'contain'.
I prefer using the containable behaviour enabled from AppModel:
var $actsAs = array('Containable');
Then you can do something like:
$cartItem = $this->CartItem->find(
'first',
array(
'contain' => array(
'Item' => array(
'Price'
),
'Address'
),
'conditions' => array('CartItem.id' => 123)
)
);
I have a weird problem with the Kohana (3.2) ORM query builder and i can't figure out what is wrong. I get "Incorrect table name" exception:
Database_Exception [ 1103 ]: Incorrect table name '' [ SELECT ``.* FROM `` JOIN `user_plugins` ON (`user_plugins`.`plugin_id` = ``.`id`) WHERE `user_plugins`.`user_id` = '9' ]
As you can see the table is empty in the query.
Controller:
$user = ORM::factory('user', Auth::instance()->get_user()->id);
if ($user->loaded() )
{
$result = $user->plugin->find_all();
}
User model:
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array( 'through' => 'user_plugins'),
);
...
user_plugin Model
class Model_user_plugin extends ORM
{
protected $_belongs_to = array(
'plugin' => array(),
'user' => array()
);
...
plugin Model
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('through' => 'user_plugins')
);
...
Anyone got any idea what could be wrong here?
Any help is very appreciated!
This is how User Model should be
class Model_User extends Useradmin_Model_User
{
protected $_has_many = array(
'plugin' => array('model' => 'plugin', 'through' => 'user_plugins'),
);
...
This is how Plugin Model should be
class Model_Plugin extends ORM
{
protected $_has_many = array(
'user' => array('model' => 'user', 'through' => 'user_plugins')
);
You don't need user_plugin Model at all, the "user_plugins" in both models refers to the table name, not the model name. Just make sure you have the table with user_plugins that have following fields,
id, user_id, plugin_id
I hope this helps.
The $_has_many, by convention, must always use plural names, unless you specify the name in $_object_name in the target model. So it should be:
class Model_Plugin extends ORM
{
protected $_has_many = array(
'users' => array('through' => 'users_plugins')
);
//...
class Model_User extends ORM
{
protected $_has_many = array(
'plugins' => array('through' => 'users_plugins')
);
//...
class Model_user_plugin extends ORM
{
protected $_belongs_to = array(
'plugin' => array(),
'user' => array()
);
//...