Where clauses: Grocery CRUD and CodeIgniter - codeigniter-2

I have two tables that need to be linked: 'user' and 'dealer'. The relation table is 'user_dealer'.
I found using $crud->set_relation_n_n() works well for edit and add screens to restrict which records are edited and added, like this:
$crud->set_relation_n_n('Dealers', 'user_dealer', 'dealer', 'user_id', 'dealer_id', 'dealer', null, array('id' => $this->session->userdata('dealer_id')));
But for some reason, this does not work for the LIST grid.
I tried $crud->where() but this does not work, as it does not have the column from the relation table there.
Can anybody help, please?

Apparently this is not possible, so I had to write a custom model that extends the Grocery Crud model and do what I needed to do.

Related

Laravel detach not working on belongsToMany

I have this model: Company
Which has a relation: $this->belongsToMany('App\CallList', 'call_list_companies', 'company_id', 'call_list_id')
And vice versa: CallList
Relation: $this->belongsToMany('App\Company', 'call_list_companies', 'call_list_id', 'company_id')
I am able to attach a Company to the CallList
But I cannot detach a Company from a CallList
Why is that?
The code I use for detaching the Company:
$company->call_lists()->detach($call_list);
I also tried the other way:
$call_list->companies()->detach($company);
It just returns null when I do it.
I checked that the both the company and the call list exists and that there was a relation between the two in the database.
Anyone have a clue what I am doing wrong?
I do not get any errors or anything either.
If it's worth mentioning, I am also using a pivot table for the relations.
Filter the relationship query down to just the call list with matching ID and then call detach().
Try:
$company->call_lists()->where('id', $call_list->id)->detach();
I had a similar or the same problem and faced it twice during last 2 years.
I figured out that the relation used soft deletion and it made this problem.
My relation belongsToMany used Pivot class.
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Database\Eloquent\SoftDeletes;
class EntityUser extends Pivot
{
use SoftDeletes; // it makes the problem
// some code
}
The table had column deleted_at. But the problem was made SoftDeletes class.
When I run $user->entities()->detach($entity) it returned 1 that means one record was touched or removed, but the table and real results had no changes.
I removed SoftDeletes and it had worked. I also removed that column from the table.
Solution
Remove SoftDeletes from the pivot class. Laravel doesn't have official support of it. https://github.com/laravel/nova-issues/issues/2750
Remove deleted_at from the pivot table. https://laravel.com/docs/9.x/migrations#available-command-aliases

Which relation to use in Laravel?

Which relation to use in Laravel to bind two table through third?
When Doctors can be assigned to some Centers. The intermediate table will be as:
doctor_id | center_id
How to create model in Laravel for this case?
You don't need a model for the intermediate table, simply use attach
Example:
$center = Center::create();
$doctor = Doctor::find(1);
$doctor->centers()->attach($doctor->id);
This is a very simple example but should give you the idea, of how to approach it.
All of it of course requires you have set up your Center and Doctor model with the correct many to many relations
Doctor.php model:
public function centers()
{
return $this->belongsToMany(Doctor::class);
}
See the documentation, for more information.
You could obviously create a model called DoctorsCenter and create it manually by doing this, whenever you want to attach a relation.
DoctorsCenter::create(['center_id' => $center->id, 'doctor_id' => $doctor->id]);
I don't see any good reason for doing this, and would not recommend it.
You can use hasMany or belongsTo relationship of Laravel.
See the laravel documentation, for more information

Laravel - set up a hasMany like relationship without the weak table

I would like your advice in the implementation of the following.
There is an Activity model and a activities table in my project, an activity can have multiple 'tips', and I need to store those tips somewhere.
Each tip has only a text description, if I wasn't using Laravel, I would simply create a table like activity_tips where I would store the id of the activity and the description of the tip, each row been one tip belonging to one activity, maybe an id incremental, but nothing more (at least what I planned).
Now, Laravel comes with this beauty called relationships, but it must be between two models.
I know I can't use something like this because it needs the Tip model (and table).
public function tips(){
return $this->hasMany('App\Tip', 'id_activity', 'id')
->select('id', 'id_activity', 'tip');
}
I could create a Tip model and table, but it doesn't feel right because the Tip table would the 'same' as activity_tips.
Is there any Laravel feature than can help me with that? Also by keeping everything in the model? Maybe use raw sql statements to only have the activity_tips
I found out about the hasMany constrain from laravel and ended up doing a tips table and Tip model.
tips: id, id_activity, tip, created_at, updated_at
public function requirements(){
return $this->hasMany('App\Tips', 'id_activity', 'id');
}
Silly me, I hope someone find this usefull
In the Activity model add:
public function tips(){
return $this->hasMany(Tip::class);
}
This assuming you have a Tip model with 'id', 'activity_id', 'tip'

Adding new columns to an Existing Doctrine Model

First of all Hats of to StackOverflow for their great service and to you guys for taking your time to answer our questions.
I am using Doctrine ORM 1.2.4 with CodeIgniter 1.7.3. I created a Site with some required tables and pumped in with datas only to realize at a later point of time that a specific table needs to have one more column.
The way i created the tables was by writing the model as php classes which extend the Doctrine_Record.
Now i am wondering if i need to just add the column in the model that requires a new column in the setTableDefinition() method and recreate that table or is there any other way that easily does this. The former method i've mentioned requires me to drop the current table along with the datas and recreate the table which i do not wish. Since doctrine seems to be a very well architect-ed database framework, i believe it is lack of my knowledge but surely should exist a way to add new columns easily.
PS: I am not trying to alter a column with relations to other tables, but just add a new column which is not related to any other table. Also i create the tables in the database using Doctrine::createTablesFromModels(); When i alter a table with a new column and run this method it shows errors.
Since you don't want to drop & recreate, use a Doctrine Migration.
The official docs here show many examples:
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/migrations/en
Since you just want to add a field, look at their second code example as being the most relevant which is like this:
// migrations/2_add_column.php
class AddColumn extends Doctrine_Migration_Base
{
public function up()
{
$this->addColumn('migration_test', 'field2', 'string');
}
public function down()
{
$this->removeColumn('migration_test', 'field2');
}
}

Relations function in AR Model, many to one relationship

So here's the scenario:
I have two table, Issue & Project.
A Project can have many Issue and an Issue can exactly one project.
Since Issue is many to one, do you have to define it?
Cause I know in Project Model I have:
public function relations()
{
return array(
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
);
}
For Issue Model I have nothing but foreign keys:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
}
I'm guessing anything to one relationship does not need to be define?
Thank you in advance.
BTW, I'm doing agile Yii book and I ended up asking myself this question.
There's a has-one option in AR class (http://www.yiiframework.com/doc/guide/database.arr).
But is this case optional for some reason?
It helps me to think of the difference between BELONGS_TO and HAS_ONE as "where is the foreign key stored"? If the Project model stored "Issue_Id" then potentially Issue could have many Projects. You use the HAS_ONE relationship to state that even if Issue COULD have many projects, it only has ONE.
However, the more common case is if you are storing the Project_Id in the Issue model (and I assume you are). Then you have to use the BELONGS_TO relationship. It appears you have defined the relationships correctly above.
Someone posted a similar question relating to Yii relations here that I helped answer:
yii - using relation HAS_ONE to get data from the related table to display in list page
As to your concern about "needing" to define relationships, you don't "need" to define any. You could write your own SQL queries to do the same thing. The ActiveRecord relations are just a convenience thing to make querying for related records simpler. If you are never going to look up an Issue's Project, then you don't "need" to define the "project" BELONGS_TO relationship.
Without actually seeing your database structure it looks to me like you have everything set up correctly. You can now easily make both $issue->project and $project->issues "lazy" relational queries, taking full advantage of the power of the Relational Active Record. Cheers and good luck with project!
In your Issue model you already have the relation specified as BELONGS_TO the project model.
I also just got the new yii-book. It helped me alot!
Happy coding :)

Resources