propelorm / Propel3

High performance data-mapper ORM with optional active-record traits for RAD and modern PHP 7.2+
MIT License
250 stars 35 forks source link

No ->save() function generated by propel build? #67

Open bushshrub opened 7 years ago

bushshrub commented 7 years ago

Hi, I'm relatively new to Propel. I setup my DB with propel init, and did propel build, which generated the models. However, when I tried to use the ->save() function, my IDE reports that the method does not exist. Does anyone know why this is so?

I'm using PHPStorm 2017.1.4

Also, my IDE also reports that another definition with the same name exists in the file, and commenting out the use myNameSpace\SubNameSpace\ModelName seemed to help.

bushshrub commented 6 years ago

bump

marcj commented 6 years ago

In Propel3 active-record methods are optional. Set activeRecord="true" at <entity> or <database> to activate it.

bushshrub commented 6 years ago

What if I don't activate it? What is the difference? And if I don't enable it, how do I persist objects to the database.

cristianoc72 commented 6 years ago

In the data mapper pattern, the persistance logic is not contained into the entity, so you should use the session objectd to do it:

// create the $configuration manually
$configuration = new Configuration(‘./path/to/propel.yml’);

$session = $configuration->createSession();

$car1 = new Car(‘Ford Mustang’);
$session->persist($car);
$car2 = new Car(‘Ferrari Testarossa’);
$session->persist($car);

$session->commit(); //Save the objects to the database, following the unit-of-work pattern