All Questions
780
questions
76
votes
2
answers
13k
views
Fat models, skinny controllers and the MVC design pattern [closed]
I just read a blog post that explains MVC with a banking analogy. I have a few months of experience with web application development with an MVC framework (CakePHP), so I get the basics, but I began ...
26
votes
2
answers
24k
views
Check if record exists in CakePHP3
In CakePHP 2 I always used empty to check if there any result.
<?php
$result = $this->Modelname->find('first', ['conditions' => ['field' => 'value'] ] );
if ( empty($result) ) {
// Bad ...
17
votes
4
answers
22k
views
Accessing Session in Model
Is there a way of accessing the current session in the AppModel class?
I want to save the ID of the currently logged in user to almost every INSERT/UPDATE action.
16
votes
5
answers
26k
views
How to use multiple databases dynamically for one model in CakePHP
Ok, my first question was modified so many times that I chose to delete it and reformulate my question. I've made a small test-case project with different model-names to find a proper solution for my ...
15
votes
3
answers
33k
views
CakePHP 3 Raw SQL Query
I'm using CakePHP 3, I need to run a raw SQL query on multiple tables. In CakePHP 2, this could be done by using the query() method on any model ( $this->Messages->query("select..") ).
I need ...
15
votes
2
answers
12k
views
cakephp one form, multiple models, not displaying one model's validation messages
I have a registration form and I am creating a record in both User and Identity tables (a user hasMany identities)
the form looks like this
<?php echo $this->Form->create('User');?>
&...
12
votes
10
answers
22k
views
CakePHP: get user info in models
I'm moving some of my find code inside models.
Previously in my controller I had
$this->Book->Review->find('first', array(
'conditions' => array(
'Review.book_id' => $id,
...
12
votes
2
answers
10k
views
CakePHP Model: COUNT(*) in Containable
I have a CakePHP 1.3 app and really enjoy the Containable behavior for fetching data.
Let's assume I have Posts in one-to-many relationship with Comments. I use Containable to query (for pagination) ...
11
votes
2
answers
10k
views
what is CakePHP model alias used for?
In user model:
var $hasMany = array(
'Photo' => array(
'className' => 'Photo',
'foreignKey' => 'owner_id',
...
),
);
In photo model:
var ...
11
votes
2
answers
5k
views
How can I tell if I'm in beforeSave from an edit or a create? CakePHP
I have a model where I need to do some processing before saving (or in certain cases with an edit) but not usually when simply editing. In fact, if I do the processing on most edits, the resulting ...
9
votes
4
answers
27k
views
Conditions in associated models using Model->find() (CakePHP)
I am having some issues with CakePHP's find() method and conditions in 'deeper' model associations. There are some of these around but I could not find an answer to this so far.
My model associations ...
9
votes
4
answers
12k
views
Paginate from within a model in CakePHP
I have a function in my Event model called getEvents - you can pass limit, start and end dates, fields, event types, and event subtypes.
I read that paginate can accept all the parameters I'm using ...
7
votes
3
answers
7k
views
cakephp - what is the difference between model and behavior?
I understand that the behavior is supposed to extend the model and add functionality to it, but in most cases the fat-model idea is making the behavior useless, isn't it?
And, even preferred, ignore ...
7
votes
2
answers
8k
views
CakePHP: Access Model in other Model / in app_model.php for Validation of Banknumber
I am wondering how I could use data from a Model B while I am validating Model A, here to check if an entered Banknumber is a correct one:
My Users specify their bankaccount during the registration. E....
7
votes
3
answers
10k
views
CakePHP: auto-set model create and update dates
Is there a way to configure CakePHP models to automatically set timestamps, such as created_on and updated_on, when saving the object?
I'm thinking of an equivalent to Django's DateTimeField's ...
7
votes
2
answers
1k
views
CakePHP has the wrong model name
I'm trying out CakePHP now and I can't get my app working because CakePHP "thinks" my model name is 'Tach' when it's in fact 'Tache'.
Why so ?
My controller is defined as :
app/controllers/...
7
votes
3
answers
4k
views
CakePHP added column doesn't appear in query
I have added a new_column to a MyISAM table on a MySQLproduction server.
I have deleted the appropriate model cache file from the /app/tmp/models folder.
Upon reloading the page and inspection, the ...
7
votes
3
answers
4k
views
In CAKEPHP can we dynamically change the table linked to a particular model?
Suppose I have 2 identical table having same structure(Call it 'tableA' & 'tableB').
I want to save certain data on table 'A' and certain data on table 'B'.
NOW I want to use the same MODEL for ...
7
votes
5
answers
1k
views
What's the correct place to share application logic in CakePHP?
I guess simple answer to the question would be a component.
Although I agree, I feel weird having to write a component for something so specific.
For example, let's say I have a table of users. When ...
7
votes
2
answers
6k
views
How to nest joins with CakePHP?
I'm trying to behave. So, instead of using following SQL syntax:
select *
from tableA INNER JOIN
tableB on tableA.id = tableB.tableA_id LEFT OUTER JOIN
( tableC INNER JOIN tableD on ...
7
votes
3
answers
2k
views
Delete multi level in cakephp ( chain delete )
I want to delete all dependent rable record
My association
Branch Model
var $hasMany =array(
'Dealbranch' => array(
'className' => 'Dealbranch',
'foreignKey' => '...
6
votes
2
answers
18k
views
CakePHP database table, missing datasource default
I found this similar question but my problem is different.
I moved my CakePHP 2.2 application to another server. There exists no problem before migration. Most of the things works fine after ...
6
votes
2
answers
4k
views
Declaration of Upload::beforeSave() should be compatible with Model::beforeSave($options = Array) [APP/Model/Upload.php, line 5]
I am being shown the following error on top of my page when using beforeSave method in my Upload model.
Strict (2048): Declaration of Upload::beforeSave() should be
compatible with Model::...
6
votes
2
answers
19k
views
Find conditions with hasMany model
I have 4 model:
Item------hasMany---->Detail
Item------hasMany---->Favourite
Item------hasMany---->Category
How can I find all Item that has Favourite.member_id = '8' and Category.item_category_id ...
6
votes
2
answers
435
views
Is it possible to create a CakePHP model that doesn't refer to a table from the database?
I'm developing an API for C2DM to send push messages to Android devices. I have the API in PHP5 and I want to tranform it to CakePHP 2.0.
How can I create a model in CakePHP that doesn't refer to a ...
6
votes
2
answers
42k
views
How to create custom MySQL queries in CakePHP?
I am trying to create my own MySQL queries in Cakephp.
This is my LocationsController.php:
<?php
App::uses('Location', 'Model');
class LocationsController extends AppController
{
public $...
6
votes
1
answer
8k
views
cakePHP Could not find validation handler
I'm struggeling with my cakePHP validation
Scenario:
In my DB I have one table "alliances" and one "federations". In "federations" connections between alliances are stored. "alliances" has got stupid ...
6
votes
1
answer
11k
views
how do i see cakephp database save errors?
if i have a cake php saveAll method like so:
if ($this->Video->saveAll($this->data)){
... // stuff that never happens, sadly
} else {
...
$this->Session->setFlash('boo! hss!...
6
votes
3
answers
6k
views
How to use: $this->Auth->user('id') in a model? Cakephp 3.0
I've been working on the skinny controller fat model way. Before, I used this in my controller:
$this
->find('all')
->contain(['Declarator'])
->where(['user_id' => $this->...
5
votes
5
answers
9k
views
Cakephp, ordering associated tables
When I search a model which "has many" of something else.
For example a blog post has many categories.
When searching for blog post with categories associated, how do I order the associated ...
5
votes
3
answers
16k
views
UPDATE query with CakePHP
I know I can use $this->Model->save() to update a particular record if I pass the id in, but how can I update a single field on that row?
I have a users table with a balance field. I want to ...
5
votes
6
answers
4k
views
Can a model link to a View in the database instead of a table in CakePHP?
I was wondering if it was possible to create a view in a database then be able to link a Model to it?
5
votes
3
answers
2k
views
filter HABTM-associated model with conditions
preamble: a few days ago I asked a question to solve a HABTM-filter, I'm not able to do it even with tutorials, so "Obi Kwan Kenobi youre my only hope".
What I want to achieve: Filtering Staff by ...
4
votes
3
answers
14k
views
How to use more than one model in a CakePHP controller
I have created a registration form and I send data to the controller.
I want to insert this data to 3 different tables (models).
How can this be achieved?
4
votes
1
answer
6k
views
cakephp component $this->controller->modelClass
In Component I try to access Myprofile Model
class SignMeupComponent extends Object
public function register() {
$this->__isLoggedIn();
if (!empty($this->controller->data)) {
...
4
votes
3
answers
4k
views
Cakephp 2.0 Localization for Model Messages
I am trying to get i18n to extract the strings from my model in Cakephp 2.0
The documentation states that
"CakePHP will automatically assume that all model validation error messages in your $...
4
votes
1
answer
8k
views
CakePHP updating multiple rows with record ids, same model
I want to update a model. I have a query like this
UPDATE mymodels SET `myfield` = 100 WHERE `id`=12 OR `id`=13 OR `id`=14
// also the same:
UPDATE mymodels SET `myfield` = 100 WHERE `id` IN (12,13,...
4
votes
2
answers
3k
views
CakePHP - Call model function from another function within in the same model
I've got a CakePHP model with a few functions in it working well. Now I'm trying to write a new function that uses a few of the functions I've already written. Seems like it should be easy but I can'...
4
votes
2
answers
2k
views
CakePHP 3 belongsToMany Validation
I am struggling with how to do validation with belongsToMany relationships. Namely, the classic recipes/ingredients relationship. I would like a recipe to always have an ingredient on create or edit....
4
votes
2
answers
11k
views
CakePHP - Column aliases in SQL queries
Is there a good way to specify column aliases when performing find() operations on a model?
$this->User->find('first', array(
'fields' => array(
'CONCAT(firstname, ' ', surname) ...
4
votes
5
answers
2k
views
Using HtmlHelper on Model to insert links in returned errors
I'm working with CakePHP and trying to understand the best ways to make my application consistent and logical.
Now I'm trying to working with Model data validation and handling validation errors in ...
4
votes
2
answers
4k
views
Cakephp Insert Ignore Feature?
Is there a way to do an "insert ignore" in cake without using a model->query function?
$this->Model->save(array(
'id' => NULL,
'guid' => $guid,
...
4
votes
1
answer
1k
views
Get the model name to implement a reusable method in AppModel in CakePHP
How can I get the current model name in AppModel?
I have this code to verify password and confirm password. But I would like to put it in AppModelp:
function isSameAs($check, $field) {
if( $check =...
4
votes
1
answer
879
views
Define a hierarchy of model classes in CakePHP
By default CakePHP has a AppModel class and every model of an application inherits from it. A common design pattern to share logic between models is to create a behavior and configure a model to $...
4
votes
4
answers
629
views
CakePHP, Email Model and Email Component. I've done something stupid
I have a model called Emails, which I have build a lot of functionality for, however now I have come to actually send emails in the emails controller and have hit a wall.
The email component clashes ...
4
votes
4
answers
3k
views
CakePHP: How can I disable auto-increment on Model.id?
CakePHP 1.3.0, mysqli
I have a model, Manifest, whose ID should be the unique number from a printed form. However, with Manifest.id set as the primary key, CakePHP is helping me by setting up auto-...
4
votes
1
answer
2k
views
CakePHP newbie question: How do I duplicate a model and its related data?
How do I duplicate an existing model record? Put another way, how do I retrieve an existing model with related data, then save a COPY of that model AND data (both model and related data are copied)? ...
4
votes
4
answers
3k
views
Manipulating Order of JOINS in CakePHP
I have the following problem with CakePHP:
In my model, Deposit belongsTo Account, and Account belongsTo Customer.
When querying Deposits, I get the Account information, but not the Customer's, by ...
4
votes
1
answer
161
views
Cakephp, validate multiple models manually
I have a form that produces the following array upon submition (see below).
I am using this data in my controller to perform several operations, after which I save each one individually. (Saving them ...
4
votes
1
answer
220
views
How do I pass multiple model IDs into comments add action in CakePHP?
My question concerns how I can use one comments controller with multiple 'belongs to' controllers.
I want to add comments to news, posts, and matches (it's a tennis website). I've got the comments ...