I'm new to ZF and apigility.
I've tried to create DB-connected service with almost zero-configuration.
Everything works well, I've got simple CRUD api, but there's one thing that I did not find solution for.
I have two tables: venues and categories
and a pivot table categories_venues to manage many_to_many relationship.
When I'm trying to create the services from tables everything works fine excepting the pivot tables. As I understood somehow because of the underscore in the name of pivot table.
So I have two questions.
How can I solve this so apigility could discover the table and import it?
How can I setup custom routes to get the listing using the following pattern /category/venue ? Is there any magic like in Rails? Can I get all the venues in the particular category? And vice versa?
Thanks.
please check here: issue:many2many relations
Related
I'm looking for a proper way to handle and store relations between data in my DB and data from a third party API. I use Laravel 5.
For example, I have a Project model (id, name). Also, I have an API which I can call from PHP and get back a JSON with Articles list.
I need to save a many-to-many relation with a junction table between Projects and Articles.
Because Articles data comes from API I do not have a local table with Articles so I can't use Eloquent to deal with relations.
My question is how to do it right.
I've thought of 2 possible solutions:
1.) Use some lib that can "map" an API as an Eloquent model. But I've only found abandoned projects. And overall this solution looks like overkill in such simple situation.
2.) Use query builder and manually handle this situation to save data about relations between Projects and Articles in the junction table.
But if I'll use this option it will be hard to deal with updates etc.
i am struggling with setting up my database and eloquent relationships in a certain scenario.
This certain part of my application will be handling online orders.
basically i want an order to consist of multiple configured items.
i want configured items to consist of a base item (ex. a cheesburger) and also of toppings.
i have gone through several scenarios, but I am trying to make this as simple as possible. here is the quick and dirty story of what I have now.
I want a configured item to consist of three things. 1. the order id of the order it is associated with. 2. the menu item that it relates to (ex. cheeseburger, hotdog ) 3. and the toppings.
I am considering two tables that are full of relatively static information about the menu items and the toppings to be referenced from the configured item table.
I had originally considered creating a new menu item on every configured item, but I like the idea of just being able to look up items/toppings and applying them to a configured item. Im sorry if this is unclear. I am three days into this and my brain is absolutely in pain by now.
here are the relationships i am considering.
configured_item: belongsTo Order; hasOne menu_Item;
Menu_item: belongsToMany configured_item; hasMany toppings;
Toppings: belongsToMany configured_item;
I guess in a way my configured item table is a pivot table of sorts, but then it will need to be referenced by an order as well.
i know questions have been asked about three way relationships, but I cant find any info on tables that are relatively static like i am trying to use.
I finally caved and used two pivot tables. it all works, but i cannot help but feel there is a better way to handle this. It seems a lot of people have similar issues and there is no clear cut solution.
New to Laravel and MVC so I'm a bit stuck and hoping I can get a solution from you.
I have 3 tables:
Users
Companies
Assets
These are all joined by pivot tables (I think that's the right term)
company_user (A company may belong to many users)
asset_user (A user may have many assets)
asset_company (A company may have many assets)
What I would like to do is get all assets for the current users company.
I manage to get the users ID. I then somehow need to use the ID to find out which company they belong to, and then retrieve all assets for that company.
I have setup the models as I think they should be. I just need to know how to get the relational data.
Any help or advice is greatly appreciated.
Thanks
You can probably use eager loading :
http://laravel.com/docs/eloquent#eager-loading
In your case:
$user = User::with('compagny.assets')->find($user_id)->get();
Where compagny and assets are the relationship function names.
I'm seeing all sorts of different ways to handle many to many relationships in Yii. However, the examples I'm seeing aren't fully fleshed out and I feel like I'm missing something.
For instance, Larry Ullman's tutorial doesn't use the self::MANY_MANY relation - http://www.larryullman.com/2010/08/10/handling-related-models-in-yii-forms/
So as far as adding and retrieving records, whats the standard way for handling Many to Many in the Model, Controller and View?
clarification: I suppose Im looking for an example involving 2 tables, related by many-to-many, where I can see not only both models, but controllers and views as well so I can fully understand whats going on.
You actually need the 3 tables (so an extra model for the pivot table), but once you have it you can actually use the regular yii relation functionality.
For example a project of mine has a many-to-many relation between a Purchase and a Transaction (please don't ask why :) ). So there is a Purchase model, a Transaction model and a PurchaseToTransaction model that establishes links between them. I can just do a $oPurchase->transactions and Yii will handle the many-to-many part using the relation, it is defined as follows:
'transactions' => array(self::MANY_MANY, 'Transaction', 'PurchaseToTransaction(purchaseId, transactionId)')
Note that for the Transactions, the same applies but the other way around:
'purchases' => array(self::MANY_MANY, 'Purchase', 'PurchaseToTransaction(transactionId, purchaseId)'),
So both $oPurchase->transactions and $oTransaction->purchases are automatically handled by Yii.
In conclusion it can indeed handle Many-to-Many using relations (at least the reading part, for writing you still need arbitrary solutions).
I am just using Yii. Just a basic idea. If you thought One to Many is not a problem then you need to create a middle table in between like for Users and Orders, create a UsersOrders table to map those keys. Then create function to get those related tables in the class like $this->UsersOrders->Orders() for function Orders in User class, vice versa.
Many to Many is actually groundup by 3 tables. 2 tables but plus hidden table in the middle.
I am new to Zend Framework and OO design patterns.
I have three tables: Owners, Shops and Products. One Owner has many Shops; and each Shop has many Products.
I have extended Zend's DB Table Abstract for each of the three tables, and setup dependent tables and reference maps.
If I want to find which Products are available from a particular Owner, I start with an Owner object and use findDependentRowset to get a rowset of Shops. I then iterate the rowset to find all Products (again using findeDependentRowset).
Is there a shortcut for doing this? How do you go about navigating three such tables using the Table Data and Row Data Gateway patterns? (Or should I be moving to a Data Mapper perhaps?)
Thanks for your thoughts!
I think what you can use is findManyToManyRowset method.
For example to find produces for a given owner you could be able do:
$productsRowset = $ownerRow->findManyToManyRowset('Products_Table_Model','Shops_Table_Model');