I'm trying to insert multiple dynamic rows to a table,
I have two tables.. 'students' and 'studentextend'..
with two primary keys (studentID in 'students) and (studentextendID in 'studentextend'), and table 'studentextend' has a field 'studentID' that is the primary key in 'students' table.
What i'm trying to do is, insert multiple rows dynamically into table 'studentextend', but each row has to have the same studentID and It's different from 'studentextendID' which is Auto increment, In other words, trying to assign multiple dynamic subject fields to one student.
But it's not inserting anything.
Controller:
$data=$this->input->post();
for($i=0; $i<sizeof($data['subject']); $i++) {
$studentExtendArray[] = array(
'studentID' => $studentID,
'subject' => $this->input->post('subject[$i]')
);
}
$this->studentextend_m->insert_batch_studentextend($studentExtendArray);
Model
public function insert_batch_studentextend($array) {
$id = parent::insert_batch($array);
return $id;
}
Related
I need to populate my database column from seeding. I have 'interested_in' column in user_profiles table and I need to populate it with according id from 'value_id' column from user_interests table. Both user_profiles and user_interests table are connected to users table ('user_id' column is in both tables). So if the value of 'value_id' column is for example 1 it needs to be 1 in 'interested_in' column, I need help on how to populate that 'interested_in' column from seeders. 'value_id' column is already populated. Here are my tables and example data and my code so far but it doesn't work currently, it shows 'Array to string conversion' error.
user_interests table
user_id field_id value_id
1 1 1
user_profiles table
id user_id interested_in
1 1 and here needs to be 1, to associate value_id (1) from user_interests table
UserProfileTableSeeder.php
class UserProfileTableSeeder extends Seeder
{
use ChunkSeeder;
public function run()
{
$users = User::all()->pluck('id');
$user_interests = DB::table('user_interests')->select('value_id')->where('field_id', 1)->get();
$user_interests_values = $user_interests->pluck('value_id')->toArray();
$seed = [];
foreach ($users as $userId) {
$seed[] = factory(UserProfile::class)->make(
[
'user_id' => $userId,
'interested_in' => $user_interests_values, // Shows array to string error!!!
]
)->toArray();
}
$this->seedChunks($seed, [UserProfile::class, 'insert']);
}
}
That's why $user_interests_values is an array as you can see in this line (at the end):
$user_interests_values = $user_interests->pluck('value_id')->toArray();
Try in the factory to change $user_interests_values to current($user_interests_values) or probably $user_interests_values['id'] or whatever.
PS: I am not really sure what's inside $user_interests_values, you can see it with a dd() and running the migration then (don't worry, the migration won't be successful because of the dd() and you will be able to run it later again until it finishes properly.
By the way, I recommend you to do something more legible than you did in your rum() method. I mean, something like this:
$interest = factory(UserInterests::class)->create();
factory(UserProfiles::class, 20)->create([
'interest' => $interest->id,
]);
I have a table with three fields
id (primary key / auto incremented)
product_name
group_id
my problem is when i insert multiple rows through form the whole group of rows should get same groupId & it should be incremented by 1 at the time of submission as there can be many users submitting the form at the same time. I dont know how to do it. Please help.
my model
function get_last_group_id() {
$this->db->select('group_id');
$this->db->from('mytable');
$this->db->order_by('group_id', 'DESC');
$this->db->limit('1');
$query = $this->db->get();
return $query->result();
}
function save_rows($ids,$product_names,$group_ids){
$this->db->trans_begin();
$ndx=0;
foreach($ids as $id){
$data = array(
'id' => $id,
'product_name' => $product_names[$ndx],
'group_id' =>$group_ids[$ndx],
$this->db->insert("product_details",$data);
$this->db->update($this->table);
$ndx++;
}
There is no need to make seperate function for getting group id and set id field also if that is auto incre
function save_rows($ids,$product_names){
$this->db->trans_begin();
$ndx=0;
$group_id = $this->db->select("MAX(group_id) as group_id")
->from("mytable")
->get()->row_array();
foreach($ids as $id){
$data = array(
'product_name' => $product_names[$ndx],
'group_id' =>$group_id['group_id']+1,
);
$this->db->insert("product_details",$data);
$ndx++;
}
You can create a new table say 'groups'
fields: id(Auto Increment) and group_name (varchar)
generate group name randomly.
Then before inserting products create a new group then you will get a new groupID that will be unique.
Then using that groupID you can carry your option forward
I am using laravel 4. I have a table called documents. I am inserting data in the Document table using Document::insert() function.
My code is given below
$statuses = array();
foreach($content as $i){
array_push($statuses, array(
'user_id' => $user->id,
'document' => $i->text
));
}
$users = Document::insert($statuses);
Now I want to get the insert ids. But the $users variable only gives me a boolean variable called true. How can I get the insert ids.
Use insertGetId()
If the table has an auto-incrementing id, use insertGetId to insert a
record and retrieve the id
Code Snippet
$id = DB::table('Document')->insertGetId($yourarray);
Note: When using PostgreSQL the insertGetId method expects the
auto-incrementing column to be named "id".
I am new in yii framework. I have three different tables in yii framework.
First Table
First table is language(id, language_name) // id is primary key.
Second Table
Second Table is verse(id,topic_id, verse_text) // id is primary key, topic_id is foreign key.
Third Table
Third table is verse_translations(id, verse_id, language_id, translations_text)
// id is primary key, language_id is foreign key references with language table, // verse_id is foreign key references with verse table.
Now My Question is.
I want to get all verse_text with specific topic_id.
So how to fetch all records from model Verse through foreign key i.e. topic_id and these record should be sent to view by data provider function.
At this time i am using.
My Controller Method
public function actionVerset()
{
$topic_id = 1;
$result=Verse::model()->with('topic_verse', $topic_id)->findAll();
$dataProvider=new CArrayDataProvider($result, array(
'id'=>'Verse',
'pagination'=>array(
'pageSize'=>2,
),));
$this->render('myverse',array('dataProvider'=>$dataProvider));
}
but it is not giving my desired result.
My Verse Model Relation Method
public function relations()
{
return array(
'topic' => array(self::BELONGS_TO, 'Topic', 'topic_id'),
'verseTranslations' => array(self::HAS_MANY, 'VerseTranslations', 'verse_id'),
'verseLanguage' => array(self::HAS_MANY, 'VerseTranslations', 'language_id'),
'topic_verse' => array(self::BELONGS_TO, 'Verse', 'topic_id'),
);
}
Any help will be appreciated.
Thanks.
To get all Verse records with a specific topic_id, this should do the trick:
$records = Verse::model()->findAllByAttributes(array('topic_id' => $topic_id));
When you have declared the relation in your model you dont need to fetch the data from db using with simply use
$result=Verse::model()->findAll();
and then when you use this
$result->topic->field_name of topic table
This will give the result of that.
I am trying to use ORM to access data stored, in three mysql tables 'users', 'items', and a pivot table for the many-many relationship: 'user_item'
I followed the guidance from Kohana 3.0.x ORM: Read additional columns in pivot tables
and tried
$user = ORM::factory('user',1);
$user->items->find_all();
$user_item = ORM::factory('user_item', array('user_id' => $user, 'item_id' => $user->items));
if ($user_item->loaded()) {
foreach ($user_item as $pivot) {
print_r($pivot);
}
}
But I get the SQL error:
"Unknown column 'user_item.id' in
'order clause' [ SELECT user_item.*
FROM user_item WHERE user_id = '1'
AND item_id = '' ORDER BY
user_item.id ASC LIMIT 1 ]"
Which is clearly erroneous because Kohana is trying to order the elements by a column which doesn't exist: user_item.id. This id doesnt exist because the primary keys of this pivot table are the foreign keys of the two other tables, 'users' and 'items'.
Trying to use:
$user_item = ORM::factory('user_item', array('user_id' => $user, 'item_id' => $user->items))
->order_by('item_id', 'ASC');
Makes no difference, as it seems the order_by() or any sql queries are ignored if the second argument of the factory is given.
Another obvious error with that query is that the item_id = '', when it should contain all the elements.
So my question is how can I get access to the data stored in the pivot table, and actually how can I get access to the all items held by a particular user as I even had problems with that?
Thanks
By default, all of Kohana's ORM models expect the table's primary key to be 'id.' You need to set $_primary_key in your model to something else.
$user_item = ORM::factory('user_item', array('user_id' => $user, 'item_id' => $user->items));
I think you need to provide a single item_id value for this to work, not an array of objects.
Also, to find all entries for a single user you should be able to do this:
$user_items = ORM::factory('user_item', array('user_id' => $user));
Does that answer your question?