php - Select an unpersisted entity -
i'm using twig loop display list of elements. these elements come decoded json array, api.
i have onetomany relation between user , these elements.
user needs chose 1 of these elements, added user addelement() function.
i tried using symfony2 form in loop, displayed on first element. tried using link controller function, since none of these elements persisted in db, got error:
"unable guess how doctrine instance request information."
here's how display elements:
{% block itinerary %} {% element in elements %} <aside class="flights-results__by-price col-md-3"> <span class="flights-results__price">{{ element.price ? element.price : 'unknown' }}</span> <a href="{{ path('selectleg', {'element': element}) }}">delete</a> </aside> {% endfor %} {% endblock itinerary %}
here function create , fill elements :
public function getavailabilities($availabilities, $planes, $airports) { $reservations = array(); foreach ($availabilities $ar) { $leg = new leg(); $leg->getid(); foreach($ar $a) { $leg = $this->fillleg($leg, $a); foreach($a->availabilities $aleg) { $leg->setairplanetype($this->findplane($planes, $aleg->airplane_type_id)); $leg->setairportstart($this->findairport($airports, $a->lfi_from)); $leg->setairportend($this->findairport($airports, $a->lfi_to)); $leg->setdurationleg($aleg->duration); $leg->setendhour($aleg->datetime_to); } $startdate = $a->datetime; } $reservations[] = $leg; } return $reservations; }
and here result when dump($elements) :
flightcontroller.php on line 55: array:4 [▼ 0 => {#953 ▼ +"3e1f975601f59090decc8f2d5ced72010162e48e": {#954 ▼ +"lfi_from": "fr58957" +"lfi_to": "fr45300" +"datetime": "2015-09-10 20:00:00" +"nb_pax": "4" +"availabilities": array:1 [▼ 0 => {#955 ▶} ] } } 1 => {#956 ▼ +"3e1f975601f59090decc8f2d5ced72010162e48e": {#957 ▼ +"lfi_from": "fr45300" +"lfi_to": "ag00060" +"datetime": "2015-09-10 23:00:00" +"nb_pax": "4" +"availabilities": array:1 [▼ 0 => {#958 ▶} ] } } 2 => {#959 ▼ +"3e1f975601f59090decc8f2d5ced72010162e48e": {#960 ▼ +"lfi_from": "fr45300" +"lfi_to": "ag00060" +"datetime": "2015-11-30 23:00:00" +"nb_pax": "4" +"availabilities": array:1 [▼ 0 => {#961 ▶} ] } } 3 => {#962 ▼ +"3e1f975601f59090decc8f2d5ced72010162e48e": {#963 ▼ +"lfi_from": "fr45300" +"lfi_to": "ololol" +"datetime": "2015-09-18 23:00:00" +"nb_pax": "2" +"availabilities": array:1 [▼ 0 => {#964 ▶} ] } } ]
the main problem api returns several thousands results. obvious reasons, cannot persist them all.
i guess easiest way ask "what best way send datas on entity function in controller, without persisting entity?". far, i've worked persisted elements, id identifier, realize gets trickier when deal non-persisted entities.
if have onetomany relation between user
, these elements
, means elements persisted somehow. why can't use id
of element ?
in case persist it, may need add paramconverter in controller code somewhere along lines:
/** * @route("/selectleg/{element}") * @paramconverter("element", class="yourbundle:element", options={"mapping": {"name": "element.whatever_param"}}) * @template() */ public function selectlegaction(element $element)
the fact is, if symfony2 doesn't know element
entity, won't able addelement()
user
.
what guess elements' list in frontend , try update user
object. in case json_encode element in twig (it's simple array after if understand) :
<a href="{{ path('selectleg', {'legasjsonstring': element|json_encode}) }}">select leg</a>
and create new element
in controller :
/** * @route("/selectleg/{legasjsonstring}") */ public function selectlegaction($legasjsonstring) { $e = json_decode($legasjsonstring); $leg = new leg(); $leg->setwhateverparameter($e->parameter_in_the_array); // more parameters here $em->persist($leg)->flush(); /// here have $leg->getid(); if ever need }
edit : adapted comment. if don't need persist element (leg) before user has chosen particular one, send element in string form in get parameter, in route parameters, or in data of post request (cleaner solution). don't need id since can pass full object in request, json string.
remove useless $leg->getid();
getavailabilities()
well, doesn't anything, , id doesn't exist anyway.
if miss point , $leg
object complicated , therefore not serializable in json, will need persist since 2 consequent requests need have access it.
Comments
Post a Comment