tags:

views:

79

answers:

1

i cant fully understand the ORM models in MVC.

so i am using symfony with doctrine. the doctrine models are created.

does this mean that i don't have to create any models? are the doctrine models the only models i need?

where should i put the code that uses the doctrine models:

eg.

$phoneIds = array();

$phone1 = new Phonenumber();
$phone1['phonenumber'] = '555 202 7890';
$phone1->save();

$phoneIds[] = $phone1['id'];

$phone2 = new Phonenumber();
$phone2['phonenumber'] = '555 100 7890';
$phone2->save();

$phoneIds[] = $phone2['id'];

$user = new User();
$user['username'] = 'jwage';
$user['password'] = 'changeme';
$user->save();

$user->link('Phonenumbers', $phoneIds);

should this code be in the controller or in another model?

and where should i validate these fields (check if it exists in database, that email is email etc)?

could someone please shed a light on this.

thanks.

+1  A: 

does this mean that i don't have to create any models? are the doctrine models the only models i need?

This depends on your definition of a model. In terms of Symfony - Doctrine classes are your model and are termed as such in the docs. In general terms though models arent just classes that deal with data persistence. They can be any domain specific classes that contain you business rules/logic. So for example your user session class apps/yourapp/lib/myUser.class.php could be called amodel, through you wont find it termed as such in Symfony.

where should i put the code that uses the doctrine models:

It depends on what youre doing. IT might be in the controller, or it could be in another model whether we are talking a Doctrine based class, or some other model class not based on Doctrine. Definitely not in the view though :-)

and where should i validate these fields (check if it exists in database, that email is email etc)?

Well normally in Symfony this happens in the form classes. For every model class generated it gets an associated form and filter class. Forms have fields and fields have validators attached. You generally set up the form in the controller then give it data and it validates and handles errors. In terms of checking if an item exists - you generally dont to do that. You try the operation, and then you catch the error and do something appropriate. If you try to add a record that already exists there will be an exception thrown (from the duplicate key error generated by your db).

prodigitalson