Questions tagged [cakephp-model]

App models are the classes that sit as the business layer in a CakePHP application.

App models classes are responsible for managing almost everything that happens regarding data, its validity, interactions and evolution of the information workflow in a CakePHP project.

Usually model classes represent data and are used in CakePHP applications for data access, more specifically they represent a database table but they are not limited to this, but can be used to access anything that manipulates data such as files, external web services, iCal events, or rows in a CSV file.

Source: Manual Reference

Example: (CakePHP 2.x)

App::uses('AppModel', 'Model');
class Ingredient extends AppModel {
    public $name = 'Ingredient';
}
501 questions
40
votes
7 answers

save() returning false, but with no error in CakePHP

My debug value is set to 2, and it's displaying all the queries, except the one I need. I have an Items controller method that is calling this method in the User model (Item belongsTo User): function add_basic($email, $password) { …
Dan
  • 401
  • 1
  • 4
  • 3
8
votes
1 answer

Retrieving inserted IDs from saveAll() in CakePHP

Using saveAll() to save multiple records in CakePHP, I am able to save them successfully in a table. But the problem arises while retrieving the IDs of those saved rows. LastInsertID() returns only a single last ID here. How can I get all the last…
Vineet
  • 287
  • 2
  • 14
7
votes
1 answer

Does CakePHP support transactions over multiple models?

I'm writing an application which supports multiple units of measurement. In the rare event a user wanted to change their system of measurement, I need to run a query which applies a multiplier to scale every unit column in the application to the…
Brad Koch
  • 19,267
  • 19
  • 110
  • 137
7
votes
6 answers

cakephp isUnique for 2 fields?

I have a registration form in which users can fill in two email address (email1 & email2). Marketing's requirement is that they need to be unique (unique as in if we had 10 users, then there would be 10*2=20 unique email address). The system is…
jodeci
  • 966
  • 2
  • 11
  • 18
6
votes
1 answer

How can I eliminate CakePHP's "SHOW FULL COLUMNS" queries?

I realize that these might not necessarily be a huge performance issue, but I'd like to get rid of any unnecessary SHOW FULL COLUMNS FROM queries that Cake generates. I've tried using the Containable behavior, which I thought would stop Cake from…
Nick
  • 1,311
  • 2
  • 10
  • 27
6
votes
3 answers

Transaction management with multiple models using single transaction commit and rollback

I am new to cakephp. I want to know if it is possible in cakephp to to handle multiple model commit and rollback with single transaction. I want to do some thing like this
Mohd Viqar
  • 258
  • 2
  • 5
  • 11
6
votes
2 answers

In CakePhp, how can I retrieve only one column from my database?

I have a very simple application with 1 table, each row contains a word, it's definition, and an example. The definition and example fields are varchars(5000). On every page, I have a sidebar which displays the list of words. After a certain number…
Sandy
  • 2,572
  • 7
  • 40
  • 61
5
votes
1 answer

How to find multiple values for Cake PHP find() method? (IN condition)

Is there a way to do a find() in CakePHP that converts to an IN condition? It seems the find() methods just take a single value to search on. I would like to do something like this: $this->User->findAllById(array(1, 5, 7)); which would convert the…
Matt McCormick
  • 13,041
  • 22
  • 75
  • 83
5
votes
2 answers

Cakephp pagination with random order?

Ok, I have looked and looked but cannot seem to find anything on this anywhere. I have a display of results that are paginated beautifully, but they currently display in ascending order. I'd like for them to display in random order. Here is my…
huzzah
  • 1,793
  • 2
  • 22
  • 40
5
votes
1 answer

How is it possible to correctly nest custom fields in a CakePHP find query?

we're often dealing with a code that looks like the following: return $this->find( 'all', [ 'fields' => ['DISTINCT Tag.tag', 'COUNT(Tag.tag) as count'], 'group' => ['Tag.tag'], 'order' => ['count' => 'DESC'] ]); This query leads us to the…
Johannes N.
  • 2,364
  • 3
  • 28
  • 45
5
votes
5 answers

How to associate model in CakePHP by fields not named by convention?

I have two tables with field username in both. How i can specify field name for both local and foreign table? I want CakePHP will do something like ON (`T1`.`username` = `T2`.`username`)` in result. Without any changes tables will be joined with…
Yaroslav
  • 2,338
  • 26
  • 37
5
votes
2 answers

Defining global conditions in Model

Is it possible to define global conditions for Model ? I have 2 Models: User and Student. In database both of them are using table users but each student has set parent_id to its owner (which is set in the same table) while each user has parent_id…
Ziemo
  • 941
  • 8
  • 27
5
votes
2 answers

cakephp foreign key not the primary key

I have a site developed in cakephp 2.0, I want to make a relation between two tables: activity_ingredients 1 id int(10) UNSIGNED No None AUTO_INCREMENT 2 type_id tinyint(2) No None 3 activity_id int(11) No None…
user1427811
4
votes
2 answers

How do I use beforeValidate() in CakePHP?

I have a form with a URL field. The default value for this field is: http://. But the field is not required. The user can skip it and submit the form. It shouldn't return an error because it's not required and because they didn't enter a URL. But…
codemonkey613
  • 960
  • 1
  • 16
  • 26
4
votes
3 answers

How to refactor better model functions in CakePHP?

I'm reading programming best practices and it is said that when creating a function we should make it do a only single specific task. I got model functions that retrieves data and it's related data.…
user133127
  • 547
  • 8
  • 19
1
2 3
33 34