All Questions
2,091
questions
578
votes
5
answers
202k
views
How should a model be structured in MVC? [closed]
I am just getting a grasp on the MVC framework and I often wonder how much code should go in the model. I tend to have a data access class that has methods like this:
public function CheckUsername($...
294
votes
13
answers
114k
views
Doctrine2: Best way to handle many-to-many with extra columns in reference table
I'm wondering what's the best, the cleanest and the most simply way to work with many-to-many relations in Doctrine2.
Let's assume that we've got an album like Master of Puppets by Metallica with ...
114
votes
9
answers
251k
views
Eloquent ORM laravel 5 Get Array of ids
I'm using Eloquent ORM laravel 5.1, and I want to return an array of ids greater than 0, my model is called test.
I have tried :
$test=test::select('id')->where('id' ,'>' ,0)->get()->...
70
votes
5
answers
33k
views
DDD and MVC: Difference between 'Model' and 'Entity'
I'm seriously confused about the concept of the 'Model' in MVC. Most frameworks that exist today put the Model between the Controller and the database, and the Model almost acts like a database ...
67
votes
9
answers
91k
views
How I can put composite keys in models in Laravel 5?
I have in my DataBase a table with two primary keys (id and language_id) and I need put it in my models. The default primaryKey in Models (Model.php in Laravel 5) is id, and I want that the ...
61
votes
14
answers
72k
views
PHPStorm is not recognizing methods of my Model class in Laravel 5.0 [duplicate]
failed insert data into database, and all query class and Model class's method not found show in IDE (phpStrom) how can I solve it?
here is my extended class (Post.php) here show error in latest and ...
59
votes
5
answers
92k
views
How to set a default attribute value for a Laravel / Eloquent model?
If I try declaring a property, like this:
public $quantity = 9;
...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I ...
57
votes
7
answers
185k
views
Class 'App\Http\Controllers\DB' not found and I also cannot use a new Model
I have very basic problem. In L4 thes below methods worked out of the box, so now I am lost. Please help. A few days ago I started a Laravel 5.0 project. I have now fresh, clean installation.
Problem ...
57
votes
8
answers
132k
views
How to delete a model using php artisan?
Is there a command to safely delete a model in Laravel 5? To create a model we use
php artisan make:model modelname
And that will create a model under app folder, and also a migration in database/...
47
votes
16
answers
187k
views
CodeIgniter PHP Model Access "Unable to locate the model you have specified"
I have been trying to load some models for this website I am building. However, for an unknown reason, it will bring the following error :
An Error Was Encountered
Unable to locate the model you ...
47
votes
4
answers
107k
views
Check if laravel model got saved or query got executed
I've seen alot of people using this way to check if a laravel model got saved. So now I wonder if it is a safe way.
And also can I check if the queries bellow got executed like this
Check if model ...
46
votes
6
answers
55k
views
Magento - Passing data between a controller and a block
Really quick and simple question but I can't find a decent answer to this - What is the best way to pass data from a controller to a block in Magento.
Incase it makes a difference, I am loading the ...
37
votes
6
answers
97k
views
Get Laravel Models with All Attributes
Is there a way to retrieve a model in Laravel with all the attributes, even if they're null? It seems to only return a model with the attributes that aren't null.
The reason for this is that I have a ...
36
votes
7
answers
49k
views
In Laravel, how do I retrieve a random user_id from the Users table for Model Factory seeding data generation?
Currently, in my ModelFactory.php, I have:
$factory->define(App\Reply::class, function (Faker\Generator $faker) {
return [
'thread_id' => 1,
'user_id' => 1,
'body' => $faker-...
36
votes
1
answer
6k
views
Best approach to model validation in PHP? [closed]
I've learned that there are often many ways to solve one programming problem, each approach typically having its own benefits and negative side affects.
What I'm trying to determine today is the best ...
34
votes
11
answers
43k
views
Models in the Zend Framework
What are some of the ways you have implemented models in the Zend Framework?
I have seen the basic class User extends Zend_Db_Table_Abstract and then putting calls to that in your controllers:
$foo ...
34
votes
4
answers
32k
views
Kohana 3: Example of model with validation
I find examples and tutorials about models and about validation. And I places that say the validation (or most of it at least) should be in the model, which I agree with. But I can't any examples or ...
33
votes
6
answers
49k
views
Laravel 5 Models Folder
I have a directory called models inside app/ where I place all my model classes. Now I want to change the directory where the following command outputs the generated classes
php artisan make:model ...
31
votes
5
answers
111k
views
Laravel how to add a custom function in an Eloquent model?
I have a Product model
class Product extends Model
{
...
public function prices()
{
return $this->hasMany('App\Price');
}
...
}
I want to add a function which will ...
31
votes
6
answers
22k
views
Laravel models: Where are model properties?
(I come from Visual Studio + Entity Framework background and trying to locate equivalent functionality in Laravel + Eloquent)
In EF and Visual Studio, we add a new Model to our application and just ...
25
votes
2
answers
19k
views
How to move the laravel 5.1 User model to \App\Models\User?
Is it possible to tell Laravel that I have moved the User.php model?
Laravel 5.1
FatalErrorException in EloquentUserProvider.php line 126:
Class '\App\User' not found
I really wasn't keen on ...
24
votes
2
answers
4k
views
Magento Proper Way To Reindex Single Item and associated Group product
What is the proper way to reindex a single item after modifications have been made to it.
Example context:
Our company relies on a third party inventory management platform called Stone Edge. We ...
23
votes
2
answers
15k
views
Yii: validation rules that always apply except one scenario
I know that you can have a validation rule that applies only for one scenario:
array('username', 'exist', 'on' => 'update'),
Now i would like to know if it's possible to do the opposite: a rule ...
21
votes
2
answers
5k
views
How to determine if a table relationship is bidirectional or unidirectional in Doctrine 2?
I am in the process of upgrading from Doctrine 1.1.4 to Doctrine 2.0.6 in my Zend application.
Currently, I am working on mapping the associations between entities. In Doctrine 2's Documentation it ...
20
votes
4
answers
22k
views
Can you access a model from inside another model in CodeIgniter?
I am writing a webapp using CodeIgniter that requires authentication. I created a model which handles all my authentication. However, I can't find a way to access this authentication model from inside ...
20
votes
5
answers
14k
views
Modeling objects with multiple table relationships in Zend Framework
I'm toying with Zend Framework and trying to use the "QuickStart" guide against a website I'm making just to see how the process would work. Forgive me if this answer is obvious, hopefully someone ...
18
votes
4
answers
13k
views
Laravel: Does every table need a model?
I am building an occasion system with Laravel. I have several tables like:
occasions
occasion_categories (cars, trikes and other)
Now i have a model called Occasion which represents the table ...
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 ...
16
votes
6
answers
36k
views
How to validate email and email already exist or not check, in Yii Framework?
How to validate email using Yii Model validation rules function code.
Also how to check email exist or not using Model validation rules function in Yii.
16
votes
1
answer
4k
views
What are the differences between Joomla Model Types?
I'm trying to get to grips with the power behind Joomla (3.x)'s framework.
I've noticed that there are multiple types of model that can be used in a component:
JModelAdmin
Prototype admin model. Acts ...
16
votes
3
answers
3k
views
Why laravel model duplicates set of data and how (if possible) to have only one set of data?
It is convenient that laravel model provides a method that it can return results from another associated table.
For example, I have a table called item and another table called feedback, where the ...
15
votes
4
answers
36k
views
How to get fillable variable as array in laravel 4.2
When we create any Model class we create protected variable fillable
I use this like
$model = new Demo;
$model->fillable // but now working
$model->fillable() // but now working.
Actually i ...
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
1
answer
15k
views
Backbone.js model.destroy() not sending DELETE request
I've been trying for days to get this working and I just cannot figure out why when I have my view to destroy a model which belongs to a collection (which properly has a url attribute for the ...
15
votes
3
answers
1k
views
Implementing a S.O.L.I.D Domain Object Model in the following project
I have the following example in which I tend to use a couple of classes, to create a simple web app.
The file hierarchy seems like this.
> cupid
- libs
- request
- router
...
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');?>
&...
14
votes
3
answers
26k
views
Yii2 before save check if attributes are changed
I want to check if some attributes in my model are changed in beforeSave() method according to data stored in database.
Is there some best practice to check if model has changed?
My goal si to make ...
14
votes
1
answer
6k
views
PHP Domain Models, DAO, and how to implement
As I asked a question about singular/plural model naming conventions yesterday, I encountered the concept of a Domain Model. As I understand it so far, a Domain Model is simply an object which ...
14
votes
1
answer
16k
views
Laravel what is use HasFactory?
I created a model in Laravel. I always noticed the default would be use HasFactory. May I know what exactly does it work. In my understanding from reading documentation, it is for linking to database (...
14
votes
2
answers
2k
views
How to share a Symfony2 model with several projects
We are creating a SaaS that monitors certain assets. This means it takes in data, saves it, and displays it in a webinterface.
For this, we have a few components that we created with/are moving to ...
13
votes
2
answers
23k
views
Laravel eloquent: Update A Model And its Relationships
With an eloquent model you can update data simply by calling
$model->update( $data );
But unfortunately this does not update the relationships.
If you want to update the relationships too you ...
13
votes
2
answers
7k
views
The difference between an Eloquent Model and a Model?
So, i'm confused with this :
In Laravel's official documentation, they say :
The Eloquent ORM included with Laravel provides a beautiful, simple
ActiveRecord implementation for working with your ...
13
votes
5
answers
42k
views
Laravel model Trailing Data when save the model [duplicate]
I have some code like this
$editStuState = StuAtt::where('studentId' , '=' , $id)->first();
$editStuState -> leave +=1;
$editStuState -> present = $editStuState -> present-1;
...
12
votes
9
answers
25k
views
Yii form model validation- either one is required
I have two fields on the form ( forgotpassword form ) username and email Id . User should enter one of them . I mean to retrieve the password user can enter user name or the email id . Could some ...
12
votes
2
answers
24k
views
How to convert model data objects array to dataProvider
Suppose I have model User which have many to many relation to itself named as friends.
so $user->friends (or $model->friends in view) gives me an array of User objects. I wanted to display the ...
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) ...
12
votes
5
answers
7k
views
Difference between Controller and Model in MVC
I'm little confused about controller and model in MVC framework (codeIgniter). Its clear to me that controller methods calls the views and Model methods interact with database.
However, I'm little ...
11
votes
13
answers
81k
views
Unable to locate the specified class: Session.php in Codeigniter
The browser:
Unable to locate the specified class: Session.php
This is my Controller:
<?php
class Chat extends CI_Controller {
public function __construct() {
parent::__construct(...
11
votes
3
answers
11k
views
Magento model won't save values
I'm working on a backend module that saves to a custom table. My problem is that I've added fields to this table as I've been working on it, the newly added fields won't save with the model ->Save() ...
11
votes
2
answers
30k
views
How to update only specific fields of an active record in Yii?
I have a model (ActiveRecord) that has 5 properties (DB columns).
I fetch a specific record and populate a form that has 3 fields (two other fields shouldn't be updated).
Then I change a specific ...