CakePHP 2.x: call virtual fields over associated model - cakephp

i have the Model ExternalShare, which hasOne SfileVersion. And a third Model Sfile, which hasMany SfileVersion.
In SfileVersion i have in $virtualFields the following code:
class SfileVersion extends AppModel {
public $virtualFields = array(
'active' => 'SfileVersion.is_active',
'externalShare' => 'ExternalShare.id'
);
[...]
}
AppModel looks like the following code, so, Containable is available everywhere:
class AppModel extends Model {
public $recursive = -1;
public $actsAs = array('Containable');
}
Now i try to call the find('all') method in SfileController, to get data from Sfile and SfileVersion:
class SfilesController extends AppController {
public function index() {
$this->Sfile->recursive = 0;
$this->Sfile->contain('SfileVersion');
$allSfiles = $this->Sfile->find('all');
}
[...]
}
Now i get the follwing error:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ExternalShare.id' in 'field list'
In the CookBook (http://book.cakephp.org/2.0/en/models/virtual-fields.html#limitations-of-virtualfields), here is
A common workaround for this implementation issue is to copy
virtualFields from one model to another at runtime when you need to
access them
, which i tried, by adding the following lines in front of the find('all'):
$this->Sfile->virtualFields['externalShare'] = $this->Sfile->SfileVersion->virtualFields['externalShare'];
But i get still the same error. I didnt directly understand, what the workaround is doing. Also, my virtual fields may not the problem (active seems to work), just the special one, which points to an other (third) Model association (ExternalShare.id). Does someone have an idea or solution? Thank you very much!

Related

How to fetch rows from hasMany relationship in CakePHP

I have the following Models:
class AppModel extends Model {
public $actsAs = array('Containable');
public $recursive = -1;
}
class City extends AppModel { // for "cities" table
public $belongsTo = 'Country';
}
class Country extends AppModel { // for "countries" table
public $hasMany = 'City';
}
..how do I fetch cities for a country. Something like this I'm trying to find out:
$countries = $this->Country->first(); // fetch a country
$cities = $country->city->find('all'); // get the cities for that country
I've set AppModel in this manner to avoid fetching cities every time I call for a country. Sometimes I don't need all the cities to be retrieved so don't want the default join. But, there are times I do want to fetch cities for a given country. The following is the only way I know how:
$cities = City->find('all', array(
'conditions' => array(
'City.country_id' => $country['Country']['id']
)
))
Is this the most convenient way to access cities once relationships have been established in the model? If so, I don't really understand why bother with $belongsTo and/or $hasMany. Thanks
Associated models first will find the main model, and then will find associated models based on the first query. So it's impossible to limit them based on associated model conditions. So, if you want to limit the main model based on the associated model, you have two options:
Do a Join find
Do a reverse find. It means that you can find City based on conditions, and contain the Country associated to it. For example (assuming that you're on CountriesController):
$this->Country->City->find('all', array(
'conditions' => array(
//your conditions
),
'contain' => array('Country')
));
As you are using CakePHP Model relationship, then it's not require to write Join query.
First thing add the following in Model of Country
class Country extends AppModel { // for "countries" table
public $recursive = 1;
public $hasMany = 'City';
}
Just write the following code,
$countries = $this->Country->find('all');
You can get the cities from $countries in an array format.

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 returning missing database table

I tried all the suggested solution for this problem which is clearing your cache folders. I also disable the cache Configure::write('Cache.disable', true); My debug level is set to 2. But I always get a same error. The table that is missing is only new added table. By the way, I'm only running in my localhost.
this is my model named Department.php
<?php
class Department extends AppModel {
public $name = 'Department';
}
?>
this is my controller name DepartmentsController.php
<?php
class DepartmentsController extends AppController {
public $name = 'Departments';
public $helpers = array('Html', 'Form','Session');
public $components = array('RequestHandler','Session');
function index() {
$this->Department->recursive = 0;
$this->set('departments', $this->paginate());
}
}
?>
Advance thank you for the answer! :D
Assuming you are actually using a database I would try the following things:
If your database table isn't named using the cakephp conventions, manually set it:
public $useTable = 'table_name';
Make sure your database config is using the right database in app/config/database.php
If that model is in a plugin, use the plugin syntax when including it in the controller:
public $uses=array('PluginName.Department');

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).

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.

Resources