Вопрос

I am new to Symfony2 and building an app based on an existing legacy MySQL schema. I've become familiar with all the Intro docs (The Book etc) but still needing to inderstand some higher level concepts of how to properly use the framework. Trying to get my head around the concept of an entity in terms of how I normally would go about writing SQL queries. I've used the CLI to generate entities for all my existing tables. As an example ... there is a Clients and a Titles entity already. Titles are 'owned by' Clients and the core Symfony annotations have mapped them correctly.

So, given a titles table with many columns of values but only one titles.client_id ... say I want to create a form action in the ClientsController (clients.yml route: /clients/{id}/add_title) that for the given client id in the url will allow the user to enter a title name and have it save a new record into titles with only the titles.name & titles.client_id values ... very simple really.

My question is ... in defining this very simple query (in normal SQL)

INSERT INTO (titles) VALUES (name, client_id)

DO I need to create another entity for titles JUST to work with those 2 specific values?

OR

What is the ideal way to use part of an entity for a specific repository ... in this case just a subset of the titles table (name & client_id)?

Here is the Action method in my Clients Controller:

//use Entity & Form namespaces for BOTH tables;

public function addTitleAction(Request $request)
{
    $client_entity = new Clients;
    $titles_entity = new Titles;
    // generate simple 2 input form with Form\TitlesType
    return etc ...
}

You may be able to tell, I also need to figure out how to work with the Form classes but my basic question here is how to generate simple queries from larger Entities and how to call from the Controllers of another Entity/Table Controller. Thx for your help.

Это было полезно?

Решение

To wrap your head around the new concepts, think of an entity as a row returned from your table. Think of a repository as your queries on the table. So you should have a Title entity (not Titles).

INSERT INTO (titles) VALUES (name, client_id)

DO I need to create another entity for titles JUST to work with those 2 specific values?

You'll want to create a new object when creating a new record (think of the new object as a new record that you then save), along the lines of:

$title = new Title();
$title->setClient($client);
$em->persist($title);
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top