many to many or one to many relationship with C5

Permalink
Hello,

I'm trying to achieve a thing which take me 10 minutes with Yii framework.

I have 2 models, lets say A and B
B has a link with B (a car has a constructor, a guy has a wife ... ok... not in this case, could be one to many..but)

I'm a little bit confused with C5 which I began to appreciate because its architecture seems to be clear. Confusing because there are models and there are pages
Some people would say, "hey create a Page" some other "create a model"...

Is there a clear manner to achieve relationship between 2 classes/models?
Is there a documentation tutorial which gives hints about that ?

Many thanjs

 
jordanlev replied on at Permalink Reply
jordanlev
It depends. What exactly are you trying to do?
ajaxian replied on at Permalink Reply
Hi,

I'm trying to have 2 models:
car and constructor
A car belongs to a model (not software model, but segment market name, as Z3 or X3 for BMW)
A constructor (Ford, Bmw, ...) has several models of car. BMW has Z3 models
I just would like to create this models and relationships and then display them.
After that creating a block for searching in the Db some cars restricted to some filters (Customers, kind of engine, kind of models)

Something quite classical i suppose (I made this on Yii, codeigniter but prefer doing this on C5 now)

Thanks
jordanlev replied on at Permalink Reply
jordanlev
First of all, the terminology you're choosing is going to make it extremely confusing for people trying to assist you (and probably for you in the future when you come back to this code later). Both "model" and "constructor" have very specific meanings in computer programming, and you should figure out some different names for your data entities (like maybe "CarMake" and "CarModel", or "CarType" and "CarManufacturer").

Anyway, here are two solutions for this situation, both of them providing a dashboard interface to edit this data (not a block that shows up on the front-end of the site... although it would be relatively easy to create that yourself once you have the data structure in place):

https://github.com/jordanlev/c5_boilerplate_crud...
(this one just uses simple database tables to represent the data, and SQL queries to combine everything)

http://www.werstnet.com/blog/using-custom-objects-and-lists-to-crea...
(this one utilizes some built-in Concrete5 functionality for sorting/filtering and storing data in a certain way -- it's more complex than the first link, but provides more functionality and so it might be better for larger projects)

If you're really just looking for how to define this data in your code and don't care about the user interface, then you can either just have two database tables and run SQL queries to JOIN them on a foreign key, or you can use the built-in ADODB ActiveRecord class to define your relationships in code. But note that ADODB ActiveRecord only handles one-to-many relationships (not many-to-many), and in my opinion this makes it not worth bothering with because it's just as easy to deal with that in SQL (just my opinion though, others feel differently).
ajaxian replied on at Permalink Reply
Hey,

Many thanks for your reply.
And yes it was confusing. Not in my mind, but for a native speaker who has to understand non english native speaker.
It was a direct and bad translation of french words ....
ajaxian replied on at Permalink Reply
Thanks for your github source code.
It is really helpfull for a C5 newcomer.

Meanwhile I have a question:
I read the doc online and there is the concept of Attribute.
AttributeKey, AttributeValue, AttributeCategories...
I thought first time I read it that it would be attribute of a class
Let's say I have a class Person, one of the attribute I can set up is name.
Which has a Type "Text".
But I do not see that in your code. You define attribute in a different manner.

Is it another way to do this or concept of attribute is something completely different ?

thanks
jordanlev replied on at Permalink Reply
jordanlev
The link I posted about "Attributes" is not written by me, and I have not used that technique so I don't know the answer. Try posting your question on his blog and maybe he can help?
ajaxian replied on at Permalink Reply
Hi Jordanlev,

I want thank you for your github links (c5_boilerplate_crud)
I downloaded it, played with it, read it.
It is very usefull to understand how the things work on C5.
Code is just well commented, things are well designed (separated).

Thanks a lot
ajaxian replied on at Permalink Reply
Hi,

I studied the Two links provided, yours and the others.
Conceptually speaking I preferes yours which resemble much more to the classic approach I used with CodeIgniter and Yii framework.
The other links contained method leads to have a lot of choice algorithm (if then else) inside controller's methods (for instance in the "view" method)

Then I decided to adopt yours...
But I'm sticked with the following problem:
You have one declared "single page" (your register one single page). and then one controller boilerplate_crud.
I would like to have one more controller in order to more clearly separate jobs (for instance to deal with another model, let's say anotherwidget.php
Otherwise for more complex application, your boilerplate_crud will explode in fly.
and then I would think that I have only to declare another single page with the diverse views.
But it fails.
Have you some hints to achieve my concerns ?
Thanks
jordanlev replied on at Permalink Reply
jordanlev
Hi,
Sorry for the delay. Yes, my boilerplate code is heavily influenced by the way that MVC frameworks such as Ruby on Rails, Kohana, and CodeIgniter are structured (and I've never used the Yii framework but based on what I've read it also seems to follow the same architectural pattern).

As for the problems you're having... yes you should absolutely be able to have more than one controller if that makes sense for your application. If it's not working, a few things to try are:

1) Make sure the class name of the controller matches the exact "path" of the file (and the corresponding single_page file). I often have a small mis-spelling or capitalize it wrong.

2) Make sure a "view.php" file exists in the appropriate single_pages directory to correspond with the controller -- even if you don't actually have a view method and don't really want that view.php file. I've unfortunately learned the hard way that Concrete5 sometimes expects that file to be there so even if it's blank, you still need it. And this seems to be something it requires upon *INSTALLATION* of the package -- so you may need to uninstall your package, fix this problem, then re-install.

If neither of those helps, can you explain the "fails" in more detail? What exactly happens? Do you see an error somewhere? Are you unable to install the package, etc.?

Also, if you can ZIP up your entire package and post it here, I can try to take a look at the code and see if I spot anything that might cause the problem.

Best of luck,
Jordan
ajaxian replied on at Permalink Reply 1 Attachment
Hi jordan,

thanks for replying.

I tried to follow strictly the C5 coding guidelines. view.php presence even if empty, naming convention ....
I achieve is :
Declaring 2 controllers, and 2 singles viewes, but this leads to have in the dashboard 2 "different areas". When I see "Pages&Themes" there are several links in this box (Themes, Attributes ...") That's what I want to reach.
But I finally come with 2 areas, one for each single page I declare.
I suppose that in order to have 2 links under a boiler_crud functionality in dashboard, one must have a different organisation in the single_pages directory.
As :
single_pages/dashboard/root_of_my_func/controller1/viewes
single_pages/dashboard/root_of_my_func/controller2/viewes
but I'm sticked with this...
Nevertheless I put zip file for your kindly suggestion of having a quick look on it.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Okay, I understand the problem. Yes, you will need to organize your controllers a little differently -- so that they match the single_page structure more closely. So what you want is a folder under "controllers" called "myboiler", then inside that folder put both of your controller files. And rename the "myboiler.php" file to just "controller.php" (because it is the "default" controller within the "myboiler" folder so C5 knows that it's the "myboiler" controller).

I might upgrade my boilerplate code to include two controllers, so that this point is demonstrated better (because it unfortunately isn't documented anywhere).
ajaxian replied on at Permalink Reply
Hi,

works fine.
I obtain my two links in the DashBoard's "Boiler plate Area" (or more depends on the number of single pages declared in controller.php for set up)

thanks to your help