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

Model relationships and queries

I have three models:

  • Country (id)
  • Location (id, country_id)
  • Objects (id, location_id)

I have defined relationships between them : location_id belongs to Location, country_id belongs to Country.

Now, when a user enters the country in a form, I want it to list all objects in that country (objects in all locations in that country). I tried it this way, according to https://docs.phalcon.io/en/latest/reference/models.html#taking-advantage-of-relationships, but it's not good:

 $country = Country::findFirst(array(  "conditions" => "id = '$entered_country'" ));

 $locations = $country->location;     

 $objects = Objects::find(array(     "conditions" => "location_id IN ('$locations->id') " ));

First two lines are good, but the third one is wrong. Is there a better way to achieve this? Is my database ralational model ok ?

Thanks



17.0k
edited Aug '14

The example you gave me is excellent, and i have tried to implement it.
I have this difficulty:

<script type="text/javascript">
        /*Define URLS to use in actions*/
        var getResultsUrl = "{{ grabResults }}";
</script>

What does it mean, define URLs to use in actions?

Also, this is not entirely the answer to my first question, so if anyone knows how to handle it, i would appreciate the answer.



98.9k

Offtopic:

I would not use the ORM as just:

 $country = Country::findFirst(array(  "conditions" => "id = '$entered_country'" ));

This is potentially insecure as it could allow SQL injections, you can better write it as:

 $country = Country::findFirst(array("conditions" => "id = ?0", "bind" => array($entered_country)));
 $country = Country::findFirstById($entered_country);


17.0k
edited Aug '14

My goal in the end is to use $query = Criteria::fromInput($this->di, 'Objects', $array); , this is an excellent feature, also quite secure.

Still;

1.how do I run a query where id=multiple values in object ? As explained in he first post Btw, I know that i can handle the first question in this way : https://pastebin.com/r6pspzkp , but isn't there a better way?

  1. As explained in second post, in this example https://github.com/phalcon/cphalcon/wiki/Dependent-Select-Dropdown , what should i do in part whih sais /Define URLS to use in actions/ ? Why are action names different? (grabResults, grabStates)

As he explains in that article, he uses grabResults to assign to endpoint to the ajax call from inside of the controller.

grabResults is just a url that you set inside your view from the controller as such:

        $this->view->setVar("grabResults", $this->url->getStaticBaseUri() . "controllerName/states" );

Thus grabResults becomes something like '//yourdomain.tld/controllerName/states'



17.0k

I just can't get it to work....

Here is my indexAction, index.volt, referrals.js and grabStates Action : https://pastebin.com/kWyt6Xey .

Can anyone see the error?

line 65 is a premature echo and will cause invalid syntax in the json.

it seems like you have many issues. perhaps you should break this down piece by piece and see what is working and what isnt. check the network tab in your browser and look at the xhr call. check to see the response is coming back as proper json. check the grabResults url to see if you properly set your base staticuri in your url component.

we can only debug so far on the internet and if you don't understand what your problems on you wont see the solutions next time you have the same problem.



17.0k

Line 65 was just a test, dont mind it.

Maybe it is my expectation that things sould work out of the box, but...

This is a official phalcon github wiki tutorial, and i expect things will work after a follow it. And that it will give me basic understanding of the topic. But this is not the case.

My honest opinion, as a newbie building a phalcon project for only 2 and a half months, is that the worst thing about Phalcon is - its documentation. And lack of good examples, and examples in general. I know that you might not understand me, but if you are starting from the bottom, this framwork is quite hard to learn. If you dont believe me, just try to follow this tutorial ''by heart'' and check how many issues will arise.

I agree with you - This framework was rather difficult to pick up at the beginning. The documentation is excellent.. the cookbooks are few and far between. Even worse is the fact that hardly anyone builds frontends using pre-generated pages from the backend anymore. Everyone has moved to using frontend SPA frameworks which makes the documentation on anything other than building an API subpar. Worse yet- most serious backend people are working with more optimized languages like Java, or new-fangled frameworks like node.

As with anything in programming - it is an uphill battle of troubleshooting stuff on your own. I myself edited that wiki I linked you a few months ago. It only worked on initial use and not for repopulating the fields. I added an entire section at the bottom to complete that functionality.

C'est la vie.



98.9k

it would be easier if you take some time to upload the project to a repository on Github instead of use pastebin or just paste some parts of the code. I personally can't understand it well there and I assume others will feel the same. It takes no time clone a repository and with a proper explanation on how to reproduce the problem people would be more willing to help you.



17.0k
edited Aug '14

Here it is: https://github.com/dextrality/dependent-dropdown .

Once again, the problem is that ater following https://github.com/phalcon/cphalcon/wiki/Dependent-Select-Dropdown tutorial, i just can't make it to work... Important files are objectsController, objects/index.volt and public/js/referrals.js



17.0k
Accepted
answer

I have manged to solve the problem(s) . Will update the wiki soon.