Insert many to many in Codeigniter with join - codeigniter

I'm using Codeiginter to implement small project, I'm stuck with many to many idea.
The problem is: If I have these entities
users(u_id,name)
cars(c_id,model)
users_cars(uc_id,u_id,c_id)
And the user number 1 choose 3 cars model using checkbox, for example (1,2,3) model1 model2 model3.
How can I implement this in Codeigniter? the (users_cars) entity should be (1,1,1)(2,1,2)(3,1,3).

I cannot comment yet so I'm writing here. But this is either just a case of multidimensional array creation or I do not understand the question
for each of the checkboxes :
users_cars = array(uc_id, u_id, c_id);

first on you create checkbox in array like
<input type='checkbox' name="cars[]" value='c_id'>
submit the form in controller and get posted data..
$cars=$this->input->post('cars');
$u_id=$this->input->post('u_id');
for($i=0;i<=count($cars);i++ ){
$insert=array('u_id'=>$u_id,'c_id'=>$cars[$i]);
$this->db->insert('users_cars',$insert);
}

Related

EmberJS 2.x , resorting or filtering records from store via actions

Consider the following code in your js file:
App.ApplicationRoute = Ember.Route.extend({
model: function(){
return this.store.findAll('team');
}
});
In your html file, you might have something like this:
{{#each model as |team|}}
<ul>
<li>{{team.name}}</li>
<li>{{team.email}}</li>
<li>{{team.state}}</li>
<li>{{team.position}}</li>
</ul><br>___<br>
{{/each}}
And the result would be something like
Bob Smith
bob.smith#bobsmithinc.com
NY
Leader
Jane Smith
jane.smith#janesmithinc.com
NY
Finance
John Doe
john.doe#janesmithinc.com
CA
Support
The question is, how would you go about sorting or filter this data after it's displayed? For example, if I want to sort by alphabetical order by name, or email, or lets say I only want to display someone in NY, and not CA.... or maybe type as I search, so if I type 'inc.com'... anyone that has that in their record will show?
My guess is this occurs in the controller via an Action? I just don't understand how to grab the store data (WITHOUT a network request) and resort or filter the data that's already there. I know you can use 'peek', but am unsure how to display the data.
to sort data in model you have to create new computed property in controller like so
sortedList: Ember.computed.sort('model', 'sortProperties'),
sortProperties: ['name:desc'], // properties to use for sorting
then in your template you can use it
{{#each sortedList as |team|}}
so then by modifying sortProperties to e.g. [state:asc] you are able to change sort order which can be in action handler

Laravel Form Model Binding with Relationships

Is it possible to bind a form with a model that has relationships? For example I have a Order model that has a one to many with a Details model. That would save a lot of time with
#foreach($order->details as $detail)
{{ Form::text('product_name', Input::old('product_name') ? Input::old('product_name') : detail->product_name)
#endforeach
For a one-to-one relation it's possible to use something like this:
Form::text('detail[product_name]')
In this case $order->detail->product_name will be populated in the given text box if an instance of Order model is bound to the from using Form::model($order) with the related model Detail but it may not possible for one-to-many because simply there will be a collection and you need a loop.
To complete the answer of #WereWolf..
Make an array of product_name detail_names
Input class allow you to access nested array by dot notation, eg: orders.1.product_name
Don't forget the second argument of Input::old() or Input::get()
is the default value, so you can specify the DB value and avoid conditional test..
.
Form::text('detail_names['.$detail->id.']', Input::old('detail_names.'.$detail->id, $detail->product_name))
In your controller, something like that:
foreach(Input:get('detail_names') as $id => $product_name)
{
//...
}
Hope this will help you to save a bit of time.

Select distinct value from a list in linq to entity

There is a table, it is a poco entity generated by entity framework.
class Log
{
int DoneByEmpId;
string DoneByEmpName
}
I am retrieving a list from the data base. I want distinct values based on donebyempid and order by those values empname.
I have tried lot of ways to do it but it is not working
var lstLogUsers = (context.Logs.GroupBy(logList => logList.DoneByEmpId).Select(item => item.First())).ToList(); // it gives error
this one get all the user.
var lstLogUsers = context.Logs.ToList().OrderBy(logList => logList.DoneByEmpName).Distinct();
Can any one suggest how to achieve this.
Can I just point out that you probably have a problem with your data model here? I would imagine you should just have DoneByEmpId here, and a separate table Employee which has EmpId and Name.
I think this is why you are needing to use Distinct/GroupBy (which doesn't really work for this scenario, as you are finding).
I'm not near a compiler, so i can't test it, but...
Use the other version of Distinct(), the one that takes an IEqualityComparer<TSource> argument, and then use OrderBy().
See here for example.

Zoho Creator: Sort sub-form records in both Main Forms and Views/Reports

Zoho Creator is a great system for quickly creating simple cloud applications. I've run into a problem with sub-forms, though: currently, Zoho Creator does not provide functionality for sorting sub-form records by a specified column. Instead, it sorts records in the order in which they were added.
My sub-form is a Creator Form that's linked to another Creator Form (basically, 2 different tables). The forms are linked with a bi-directional lookup relationship.
I've seen and tried implementing these "hacks", but none of them work for my situation:
[Zoho Forums, "Subforms sorting rows"][1]
[Zoho Forums, "Hack to sort rows of a subform and pre-populate row fields that I want to preset"][2]
I also called Zoho tech support, and after looking at my application, they said that sorting sub-form records is not currently possible.
Any other ideas?
My tested solution is still a hack, but until Zoho implements a method to sort sub-form records via the GUI, this will have to do.
First, create a function that you can call from anywhere (e.g. when a new sub-form record is added or changed)--for details on that, go here: http://www.zoho.com/creator/help/script/functions.html
This function will first duplicate the sub-form records by the parent record ID (sorting by the appropriate column) and then delete all sub-form records that were inserted before the script started:
int SubFormRecords_SortByAnything_ReturnCount(int ParentRecordID)
{
scriptStartTime = zoho.currenttime;
for each rSubFormRecord in SubFormRecords [ParentFieldName = input.ParentRecordID] sort by FieldName1, FieldName3, FieldName2
{
NewSubFormRecordID = insert into SubFormRecords
[
FieldName1 = rSubFormRecord.FieldName1
FieldName2 = rSubFormRecord.FieldName2
FieldName3 = rSubFormRecord.FieldName3
];
}
delete from SubFormRecords[ (Series == input.ParentRecordID && Added_Time < scriptStartTime) ];
return SubFormRecords[ParentFieldName == input.EventID].count();
}
Once the above sorting function is in place (customized for your application), call it when appropriate. I call it when adding a record associated with the sub-form, or when I change the sorting column values.
That works well, and as long as you don't have complex logic associated with adding and deleting records, it should have minimal impact on application performance.
Please let me know whether that works for you, and if you have any better ideas.
Caveat: This solution is not suitable for forms containing additional sub-form records because deleting the records will delete linked sub-form values.
Thanks.
I have a a very simple workaround:
1) You have to add a Form Workflow
2)Record Event - Create OR Edit OR Create/Edit (As per your requirement)
3)Form Event - On successful form submission
4)Let Main_Form be the link name of the Main Form
4)Let Sub_Form be the Link name of the Sub Form (Not the link name you specify in the main form for the same sub form)
4)Let Field1 and Field2 are fields of subform on which you want to sort subform records
5)Let Link_ID be lookup field of Mainform ID in the subform
Workflow
1)Sub_Records = Sub_Form[Link_ID == input.ID] sort by Field1,Field2;
(sort by multiple fields, add asc/desc as per requirement)
2)delete from Sub_Form[Link_ID == input.ID];
3)for each sub_record in Sub_Records
{
insert into Sub_Form
[
Added_User = zoho.loginuser
Link_ID = input.ID
Field1 = sub_record.Field1
Field2 = sub_record.Field2
]
}
//Now you check the results in edit view of the main form

Display Methods - Multiple Form Data Sources

This may seem a simple question, but for some reason I am vexed.
I have a form with 3 datasources - InventTable, InventSum, InventDim.
So, for example, my grid shows;
Item, Name, Site, Warehouse, Physical Stock
I have placed a display method on InventDim form DataSource, but I need access to the ItemId from either inventTrans or InventSum. (Obviously looking for the "current" itemId).
All I can access is the inventDim which is passed as a parameter _inventDim, as standard.
What is the best way to access the "current" itemId?
Okay, I found the answer, with great thanks to this reference by Joris de Gruyter;
http://daxmusings.blogspot.co.uk/2011/10/forum-advanced-display-method-querying.html
The key was to put the display method on the InventSum datasource.
You can then use _inventSum.joinChild() to retrieve the linked inventDim, here is Joris' example;
display Qty AvailPhysical(InventSum _inventSum)
{
InventDim joinDim, dimValues;
InventDimParm dimParm;
InventSum localSum;
//THE IMPORTANT LINE...
dimValues.data(_inventSum.joinChild());
dimParm.initFromInventDim(dimValues);
select sum(AvailPhysical) from localSum where localSum.ItemId == _inventSum.ItemId
#InventDimExistsJoin(localSum.InventDimId, joinDim, dimValues, dimParm);
return localSum.AvailPhysical;
}
I am sure this will help someone out in the future!

Resources