We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Why when i use same model ! the source table is the same one !

Hello , i just want to know

why when i use the same model but in diffirent variables ! (mean $a = new class , $b = new class)

but at end $b source table is the same as $a table

i dont understand where that come !!

i use a model base for those two models

maybe at the time of waiting i will found a solution

) else i will go back to this topic ^_^ !

Thanks



125.7k
Accepted
answer

if $a and $b are different objects, but use the same class, it makes sense they would use the same source table, because they're the same type of thing.

$A = new Car();
$B = new Car();

$A and $B are different objects, but are both instances of Car. If Car extends Phalcon\Mvc\Model, then Phalcon will automatically infer the table name from the name of the class. Since the class is the same for both $A and $B, then necessarily they will have the same source table.

Okey ! that make sense

because my project use a dyanmic model that mean a model for all tables in a db

so i will need to change every time the source of model :D

Thanks you for your answer ^_^!

Greetings.

edited Jan '20

I don't see much of an advantage to using 1 class for all tables. It would be much easier and cleaner if you followed the MVC paradigm and had a separate class for each table. The amount of code you need to write for each model is really minimal, so it wouldn't be too much work - certainly less work than always needing to dynamically set the source of the model.

If you have common functionality you want all your models to have, then use a base model that all other models extend. Like this:

class BaseModel extends \Phalcon\Mvc\Model{

// common model methods here

}

class CarModel extends BaseModel{
// This class gets all the common functionality defined in BaseModel, 
//as well as the functionality provided by \Phalcon\Mvc\Model
}

:) i understand your idea

but the problem

any one can create a thousand of tables

so it not possible to create a model for every table :)

that is the problem

but i found a solution :) it all good now ^_^

thanks



10.1k

Are you talking about tables or rows? Models are primarily used for managing the rules of interaction with a corresponding database table. In most cases, each table in your database will correspond to one model in your application.

https://docs.phalcon.io/4.0/en/db-models#models

you know

the are a system that

when you chose a table

for exemple :

domain.com/?table=users

i want to use a model dynamic for all tables :)

i dont want to create each model for every tables

because its depend by which tables

and has a diffirents columns and rows :)

have you understand now ?

)