define model association without the second model - cakephp

Is there a way in CakePHP 1.3 to define a model association without having a model for the associated table? For example:
<?php
class SomeModel extends AppModel
{
var $useTable = 'some_table';
var $belongsTo = array(
'AnotherModel' => array(
// association data here
)
);
}
?>
Where AnotherModel doesn't actually have a model file. I just want to define the table that model would use and the association details. Is this possible?

Quick answer is: It should be fine. Have you tried it? Worse case scenario is it won't work without the file, so just add the model file. It takes all of two seconds:
<?php
class AnotherModel extends AppModel {
var $name = 'AnotherModel';
}
?>
Done!
UPDATE
If you follow cake convention on the naming of tables, you should be able to reference the table using the appropriate name without the model file. For example:
my_models = MyModel
your_models = YourModel
model_tables = TableModel
However, if you have a table that does not follow convention, you must create a model file that defines $useTable to indicate which table it relates to:
some_table = model file: SomeTable where $useTable = 'some_table';
another_model = model file: CustomModel where $userTable = 'anotherModel';
There is no other way around it. CakePHP is not magic. It needs to know what table is being referenced. Unless you are doing joins. Then in the join you can reference the table.

Related

CakePHP: create temporary table and use it

I'm trying to create a temporary table within CakePHP 2.x, but I always receive the message "Error: Call to a member function query() on a non-object".
I did some research and found a solution, but this one is not working for me: Create temporary table in CakePHP and load it as a Model
EDIT:
The following code now produces a different error: "Error: Table devices_cls for model DevicesCl was not found in datasource default."
Here is my code:
class DevicesController extends AppController {
public $uses = array('Device','Client');
public function index(){
$conditions = array();
$tmpModel = 'DevicesCl';
$tmpTable = 'devices_cls';
$this->loadModel('DevicesCl');
$this->DevicesCl->query("CREATE TEMPORARY TABLE IF NOT EXISTS devices_cls AS (SELECT uuid,ownerUuid,status,os,imei,updatedOn,msisdn,model FROM device)" );
As I'm quite new to CakePHP, do I need to add an additional model class? I don't think so, as this should be handled by the temporary table - right?
Thanks for your support!
class DevicesController extends AppController {
public $uses = array('Device','Client');
public function index(){
$conditions = array();
$tmpModel = 'DevicesCl';
$tmpTable = 'devices_cls';
$this->loadModel('DevicesCl');
$this->DevicesCl->useTable=false;
$this->DevicesCl->query("CREATE TEMPORARY TABLE IF NOT EXISTS devices_cls AS (SELECT uuid,ownerUuid,status,os,imei,updatedOn,msisdn,model FROM device)" );
$this->DevicesCl->useTable = $tmpTable;
The problem is that the Model "DevicesCl" does not exist yet.
in your example on the link : the Model exist
so if you change
$this->DevicesCl->query(...)
to
$this->Model->query(...)
But i don t see why you need a functionnality like that... i think there are surely best solutions no ?
In Cakephp 3, you can do the following:
Model/Table/MyTempTable.php
<?php
// ......
class MyTempTable extends Table{
public function initDownload(){
$connection = ConnectionManager::get('default');
$connection->execute('CREATE TEMPORARY TABLE temptemp as (select * from refernece_db)');
// Do your thing
}
}
// When use the temp table, first should do:
$this->MyTempTable->initDownload();
// Then, can use Cakephp3 Table syntax normally as if it is a normal table

Table not found, same using useTable

I have a model, called "Cliente" and this model have a association with another table called ClienteRelFot. I declared that ClienteRelFot has a useTable = 'rel_fot_ec', but the cake are looking for "rel_fots".
The rel_fot_ec table exists on my database because I use to find another data.
Someone have a idea to solve this problem?
I tried clear cache and delete all files from tmp folders.
Below, we have the error:
Error: Table rel_fots for model RelFot was not found in datasource default.
Your associations are trying to pull data from the model 'RelFot' (per the error), not 'ClientRelFot', so declaring that 'ClienteRelFot' uses the table 'rel_fot_ec' will have no effect.
Try adding:
public $useTable = 'rel_fots';
in your 'RelFot' model.
I had this problem too, even though using public $useTable = ...
My data model: Event hasMany > Submissions hasMany > Authors
Cake was telling me [MissingTableException] Table authors for model Author ..., the problem was not in the Author model, but in the Submission model:
class Submissions extends AppModel {
public $hasMany = array(
'Author' => array(
'className' => 'authors', // author should be singular
'foreignKey' => 'submission_id'
)
);

Cakephp Model Association Not kicking in

//index.ctp, this forms points to action updateData in profilesController
$this->Form->input('User.lastname');
$this->Form->input('Profile.age');
$this->Form->input('Profile.height');
$this->Form->input('Associate.city');
$this->Form->end('Submit');
//user.php
Class User extends AppModel {
var $hasOne = array('Profile', 'Associate'};
var $primaryKey = 'user_id';
}
//profile.php
Class Profile extends AppModel {
var $belongsTo = array('User');
var $hasOne = 'Associate';
var $primaryKey = 'user_id';
}
//associate.php
Class Associate extends AppModel {
var $belongsTo = array('User');
var $primaryKey = 'user_id';
}
//profiles_controller.php
Class ProfilesController extends AppController{
function updateData(){
//output incoming request for debugging purposes
debug($this->request->data);
//here i fetch the db to get the id of user
$options =
array('conditions' => array('User.username' => $this->Auth->user('username')),
'fields' => array('User.id')
);
//find user id so we can find user in related tables
$id = $this->Profile->User->find('first', $options);
//here I modify request data so cakephp finds the users through primaryKeys
$this->request->data['Profile']['user_id'] = $id['User']['id'];
$this->request->data['Associate']['user_id'] = $id['User']['id'];
$this->request->data['User']['id'] = $id['User']['id'];
if($this->request->is('post'){
//this updates data in table no problem
$this->Profile->save($this->request->data);
//this updates data in table no problem either
$this->Profile->Associate->save($this->request->data);
//this returns false...it breaks here
$this->Profile->User->save($this->request->data);
}
}
}
Table structure:
User
|id|int|auto increment
|firstname|varchar
|lastname|varchar
|date|timestamp
Profile
|id|int|autoincrement
|user_id|int
|age|int
|height|int
Associate
|id|int|autoincrement
|user_id|int
|city|varchar
|country|varchar
Ok I know what some of you might tell me, why do I do this on the profilesController and
not on the UsersController. Well, my idea is to separate some actual important user
data from the profile data so it's my intention to write the code for profile on the ProfilesController...as I was developing I was assuming that the same Model association would have automatically updated the User.lastname field in the User table..but that is the part where my code breaks and I have tried but I can't make it work
The current association in my mind at least is as follows:
User has one Profile
User has one Associate
Profile belongs to User
Associate belongs to Profile and User
Can anyone tell me what am I doing wrong? i am following what I think is a logical approach for my application, cakephp updates Profile and Associate models but User remains unaffected.
Assuming the primaryKey of your users table is 'id', just remove all of the $primaryKey lines, and try again.
The only reason to set the primary key is if it doesn't follow the default that CakePHP has in place. I would GUESS (can't see your tables) that the primaryKey field in your 'users' table isn't 'user_id' - more likely it's just 'id', and in the other tables, it's 'user_id'. If that's the case, there's no need to specify the $primaryKey, since that's the default of CakePHP.
As it turns out after reading the cakephp documentation (and obviously being a n00b) the reason why my code was breaking is because I had a callback beforeSave in my model. I didn't know that in order to save data I had to disable the callback which was unrelated to the part of the code I presented to you. The solution in a case like this is to do as follows:
$this->Profile->User->save($this->request->data, array('callbacks' => false));
I don't know you guys but sometimes I feel the cakephp documentation is a little too simplistic, I discover this by looking at the API.

cakephp behavior afterFind not called on related models

I am using an afterFind function to modify data from a find function. It works fine. If I move the afterFind function into a behavior (in a plugin) it still works, but only when the model of interest is the primary model, i.e. it isn't called when the model belongsTo another model. Is there any way round this? I'm using cake 1.3.4. This is a simplified version of the behavior:
class ChemicalStructureBehavior extends ModelBehavior {
function afterFind(&$model, $results, $primary) {
foreach ($results as &$unit) {
// format chemical formula (with subscripts)
$unit[$model->alias]['chemical_formula_formatted'] = preg_replace('/([0-9]+)/i', '<sub>$1</sub>', $unit[$model->alias]['chemical_formula']);
}
return $results;
}
}
I guess I'd do one of 2 things depending on how generically the code block applies:
Universal version: not use a behavior, but include your method block in AppModel::afterFind
Surgical version: use a behavior and attach it to each model that needs to share the functionality.
A behavior isn't supposed to work on related models, for example, if you have this two models:
app/models/product.php
<?php
class Product extends AppModel{
var $belongsTo = array('Category');
var $actsAs = array('SomeBehavior');
}
?>
app/models/category.php
<?php
class Category extends AppModel {
var $hasMany = array('Product');
}
?>
SomeBehavior will only be executed when calling methods for Product, because the behavior isn't associated with Category
http://github.com/m3nt0r/eventful-cakephp
Set up an event that does the formatting - trigger that event however you need to. Easy as Cake.

CakePHP requiring a database table

I intended to create a controller that handles a contact page. I created the controller ContactsController. The problem is that it is asking for a table with the same name:
Missing Database Table
Error: Database table
username_contacts for model Contact
was not found.
Notice: If you want to customize this
error message, create
app/views/errors/missing_table.ctp
Do I really need to create a table with no data for this?
This is my controller code:
<?php
class ContactsController extends AppController {
var $name = 'Contacts';
function index($id = null)
{
$this->set('page', ClassRegistry::init('Page')->findByShortname($id));
}
}
var $name = 'Contacts';
var $uses = array();
not to be that guy, but this is documented well.
http://book.cakephp.org
You might want to create the model anyway as you'll almost certainly find you need to do some database type stuff. It doesn't need to use a db_table:
class ModelWithoutTable extends AppModel
{
var $useTable = false;
}
Think "fat model - thin controller"

Resources