I google and google and read 100000 tutorials but i think this is inposible to do in codeigniter on model, controller and views.
Am tring to show my database records like this :
Default Category
|----- Sub category
| ----One more category
|----- Somthing else
I try with LFT and RGT cols but i realy dont much understand that concept.
I read this and try that function to optimize in Codeigniter model but that only work in model.
http://www.sitepoint.com/hierarchical-data-database-2/
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/
I now have simple db sheme : Categories with cols id, parent_id, title
Any1 can give me one simple example... Please
Thanks
Assuming the follow basic table structure:
id
parent_id
name
You need to do a nested loop.
Something like:
SELECT * FROM table WHERE parent_id = 0; # First level
SELECT * FROM table WHERE parent_id = ?; # Subsequent levels
Normally you would then build this into an object in your model which would return something like an array with children in it. We would normally do this with a function that calls itself and returns an object that we append to the previous object. Watch out with recursive functions though, they have a tendency to go wrong on you!
Hope that helps.
Related
Maybe simple, but I can't figure it out...
When I create a record using Eloquent and a model that extends Model, and then get its id right after it just works:
$example = Example::create(['name'=> 'exie']);
dd($example->id);
// returns id (ex. 15) as expected from the created record...
When I create a record using a model that extends Pivot and try to get id, it only returns null.
$customPivotExample = CustomPivot::create(['name' => 'custie']);
dd($customPivotExample->id);
// returns null instead of id...
The records all have a PK so I expected to just get the ID back, but apparently there is something about using a custom pivot model and getting it's id after creation what I am overlooking..
(examples are really simple but the actual code only contains more key=>value pairs and nothing more)
anyone has any idea?
Own Answer
Putting this here because this is not written (somewhat) in the Laravel documentation.
They mention this about auto incrementing ID's:
https://laravel.com/docs/9.x/eloquent-relationships#custom-pivot-models-and-incrementing-ids
I had not done this (my bad), but doing this also enables getting the ID after creation of a pivot record as in my second example....
Using Laravel + Voyager, I have a "Has Many" relationship:
Course hasMany Teachers.
In backend all is fine, but if I try to get the information in the frontend, I only get the value in table, not the relation, so the output goes something like:
0
id 1
teachers_id null
name "Math"
1
id 2
teachers_id null
name "English"
Current code in web routing:
Route::get('/course', function () {
$co= App\Course::all();
return $co;
});
How can I get the correct output?
teachers_id Xav, Titus
so like #Tpojka commented you can start by eager loading the relationship when you are initializing the course likeso $co = App\Course::with('teachers')->get(); after which you do not need to make another unnecessary call to your database for the teachers of that course. You can get a collection of all the teachers of that course by simply calling $teachers = $co->teachers; here $teachers is now a laravel collection you can simply loop through it on the client side and display the information about the teachers you want to display. I hope this helps.
Good luck and happy coding. :)
i have 2 tables, stores and products
stores table has field called products_ids
in this case i am saving the products in the stores by their ids in products_ids field as an array like this [1,2,3,4,5] i know it's not good practice to do it like this but this is the situation.
how can i make a relation in the model to achieve thing like this
Store::with('products')->get();
thanks
I don't know if this would work for you, but try it anyway:
in your Store model add the following:
public function products ()
{
return Product::whereIn('id', $this->products_ids)->get();
}
I have been trying to join two custom table using magento's commands. After searching i came across this block of generic code
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join( array('table_alias'=>$this->getTable('module/table_name')),
'main_table.foreign_id = table_alias.primary_key',
array('table_alias.*'),
'schema_name_if_different');
Following this as template I have tried to join my tables together but have only returned errors such as incorrect table name or table doesn't exist or some other error.
Just to clear things up can someone please correct me on my understanding
$collection = Mage::getModel('module/model_name')->getCollection();
Gets an instance of your model. Within that model is the table that holds the required data (for this example I shall call the table p)
$collection->getSelect()
Select data from table p
->join()
Requires three parameters to join two table together
PARAM1
array('table_alias'=>$this->getTable('module/table_name'))
'the alised name you give the table' => 'the table you want to add to the collection (this has been set up in the model folder)'
PARAM2
'main_table.foreign_id = table_alias.primary_key'
This bit i don't get (it seems straight forward though)
my main table (p) doesn't have a foreign id (it has it's primary key - is that also its foreign id)?
has to be equal to the alised name you gave in param1
PARAM3
'main_table.foreign_id = table_alias.primary_key'
get all from alised name
Where have I gone wrong on my understanding?
Please have a look in below sql join statement, I am using it in my project and it is working perfectly.
Syntax
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'table_name_for_join', 'main_table.your_table_field ='.Mage::getConfig()->getTablePrefix().'table_name_for_join.join_table_field', array('field_name_you_want_to_fetch_from_db'));
Working Query Example
$collection = Mage::getModel('module/model_name')->getCollection();
$collection->getSelect()->join(Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar', 'main_table.products_id ='.Mage::getConfig()->getTablePrefix().'catalog_product_entity_varchar.entity_id', array('value'));
Hope this will work for you !!
In Symfony 2 book there is an example of how to do that for ONE $product: http://symfony.com/doc/2.0/book/doctrine.html#fetching-related-objects
It is quite simple:
public function showAction($id)
{
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
$categoryName = $product->getCategory()->getName();
// ...
}
But what if i want to fetch ALL products with category info joined automatically to each project?
Thank you!
This will do the trick:
$products = $this->getDoctrine()->getRepository('AcmeStoreBundle:Product')->findAll();
However, each time you do a getCategory on a product a sql query will be triggered which could result in performance issues.
What you really want to do is to make yourself a ProductManager service and write an explicit query joining product and category. So only one sql query will be generated. The Doctrine 2 manual has plenty of examples.
http://docs.doctrine-project.org/projects/doctrine-orm/en/2.1/reference/query-builder.html
You can either iterate over the products and get the categories from there. Adjusting the fetch mode on the relations might help in reducing the amount of queries performed.
However, you might also just write a custom DQL query to get what you need. It might look like something like this:
SELECT p, c
FROM AcmeStoreBundle:Product p
INNER JOIN p.category c