how to save id of one table to another table in codeigniter? - sql

i want to save id of one table to another table how should i write the model and controller.
For example : I have a database like this
tbl1: (id,content,time,date,category)
tbl2: (id,tbl1_id,category_detail)

$tbl1Data = array(
'id' => '',
'content' => 'Hi, content will be here',
'time' => date("H:i:s"),
'date' => date("Y-m-d"),
'category' => 'Electronics'
);
$this->db->insert('tbl1',$postData);
$recordId $this->db->insert_id();
$tbl2Data = array(
'id' => '',
'tbl1_id' => $recordId,
'category_detail' => "alskjdflkajsdlk falsjdflka lsdkfj as",
);
$this->db->insert('tbl1',$postData);

Related

How to get record with same fixture in Yii unit test

Its a parent child relation,
In childGroup1, getting error during accessing 'PARENT_ID' attribute.
The given error is Trying to get property of non-object.
I am having access dynamically.
How to get PARENT_ID in such case.
return array(
'group1'=>array(
'ID' => 1,
'NAME' => 'Test',
'STATUS' => 1,
),
'childGroup1'=>array(
'ID' => 2,
'PARENT_ID' => $this->getRecord('groups','group1')->ID,
'NAME' => 'Child Test group1',
'STATUS' => 1,
),
);
Since the records are not loaded yet, you cannot use $this->getRecord() to acquire a record. As such, just use plain old array logic to get the record's ID.
$records = array();
$records['group1'] = array(
'ID' => 1,
'NAME' => 'Test',
'STATUS' => 1,
);
$records['childGroup1'] = array(
'ID' => 2,
'PARENT_ID' => $records['group1']['ID'],
'NAME' => 'Child Test group1',
'STATUS' => 1,
);
return $records;
If you need records from other fixtures, just require them.
$groups = require __DIR__.'/group.php';
This, of course, would be what you put at the top of files OTHER than groups.php, in order to gain access to the groups models.

Create New row using bootstrap widgets TbSelect2

Hello I am using bootstrap.widgets.TbSelect2 in Yii.
You can see this link to check my form.
https://www.diigo.com/item/p/qoaqepdzbsbbaqeqdzbcbrsdpa/bc91ebd999371bef68737b8320afed91
How to add the data in TbSelect2?
thank you.
its so simple , just generate an array for this
$this->widget('bootstrap.widgets.TbSelect2', array(
'asDropDownList' => true,
'name' => 'status',
'data' => array( // свои значения для option
'active' => 'Active',
'pending' => 'Pending',
'invited' => 'Invited',
'deleted' => 'Deleted'
),
)
);

How to add where in Yii model search method?

I have four tables in my DB as shown(user, user_test, test, status). There is a M:M to relation between user and test and therefore user_test is a Gerund between them.
The status table has statuses for both user_test and test tables. The tablename field in status table shows which table the status belongs to as shown in the lower image.
I want to show table user_test in a CGridView with the related data in all the three tables user, test and status. All is well as the relations are correct.
Problem: When I want to show status.name in my CGrid like:
array(
'header'=>'Status',
'value'=>'$data->status->name',
),
It gives me name 'completed' but it is the status of table 'test' and the correct one should be 'confirmed' as status_id in user_test is 2.
Any help?
Database design
Status table
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with=array('user','test','status');
$criteria->compare('id',$this->id);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('test_id',$this->test_id);
$criteria->compare('status.id',$this->status_id);
$criteria->compare('bonus',$this->bonus);
$criteria->compare('user.signum',$this->signum, FALSE);
$criteria->compare('user.email',$this->email, FALSE);
$criteria->compare('test.seats',$this->seats, FALSE);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'attributes'=>array(
'signum'=>array(
'asc'=>'user.signum',
'desc'=>'user.signum DESC',
),
'email'=>array(
'asc'=>'user.email',
'desc'=>'user.email DESC',
),
'seats'=>array(
'asc'=>'test.seats',
'desc'=>'test.seats DESC'
),
'*',
),
),
));
}
UPDATE:
Relations are here:
//UserTest
....
return array(
'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
'test' => array(self::BELONGS_TO, 'Test', 'test_id'),
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
'testtimeslots'=>array(self::BELONGS_TO, 'TestTimeslots','timeslots_id'),
);
// Test
return array(
'testType' => array(self::BELONGS_TO, 'TestType', 'test_type_id'),
'status' => array(self::BELONGS_TO, 'Status', 'status_id'),
'testCriterias' => array(self::HAS_MANY, 'TestCriteria', 'test_id'),
'testTimeslots' => array(self::HAS_MANY, 'TestTimeslots', 'test_id'),
'userTests' => array(self::HAS_MANY, 'UserTest', 'test_id'),
);
// User
return array(
'userLanguages' => array(self::HAS_MANY, 'UserLanguage', 'user_id'),
'userTests' => array(self::HAS_MANY, 'UserTest', 'user_id'),
);
// Status
return array(
'tests' => array(self::HAS_MANY, 'Test', 'status_id'),
'userTests' => array(self::HAS_MANY, 'UserTest', 'status_id'),
);
UPDATED: The sql I want to generate is:
SELECT * FROM user_test AS UserTest
INNER JOIN user ON user.id=UserTest.user_id
INNER JOIN test on test.id=UserTest.test_id
INNER JOIN (SELECT status.value, name from status where status.tablename='user_test') AS Status ON (Status.value = UserTest.status_id)
Add before the compare part
$criteria->addCondition('Where a = b');
http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addCondition-detail
It takes an overload of and or or like so
$criteria->addCondition('Where a = b', AND);
$criteria=new CDbCriteria;
$criteria->condition="user_name='john' and user_status='active'";

like OR operator in Cakephp

I am new to cakephp and I want to add or, and, and like to my existing query.
I want to make a condition like this
WHERE 'Message.user_id = Contact.user_id' AND 'Message.mobileNo LIKE'=>"%Contact.mobileNo" OR LIKE'=>"%Contact.homeNo" OR LIKE'=>"%Contact.workNo"
My query is
$this->bindModel(array(
'belongsTo' => array(
'Contact' => array(
'className' => 'Contact',
'foreignKey' => false,
'conditions' => array(
'Message.user_id = Contact.user_id',
'Message.mobileNo = Contact.mobileNo'
),
'type' => 'inner'
)
)
), false);
$this->find('all', array('conditions' => array(),
'fields' => array('DISTINCT mobileNo')));
you can use like below in your existing query.
$this->find('all', array(
'conditions' => array(
'OR' => array(
array(
"Message.mobileNo LIKE" => "%Contact.mobileNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.homeNo",
),
array(
"Message.mobileNo LIKE" => "%Contact.workNo",
)
)
),
'fields' => array('DISTINCT mobileNo')
));
And you can also refer Detail Document for simple search with like
you can use: for "like"
$this->Post->find("all",array('condition'=>array('Author
LIKE'=>"ad%")));
above query will give You the data from table posts where author name starts with "ad".
for "OR"
$this->Post->find("all",array('condition'=>array("OR"=>array('Author
LIKE'=>"ad%",'Author LIKE'=>"bo%"))));
above query will give You the data from table posts where author name starts with "ad" OR "bo"
This blog post can b useful to u as well!

How to do this query condition in cakephp?

How can I make this query the CakePHP way?
SELECT *
FROM uploaded_sales us, sales s
WHERE us.item_id = s.audience_id
The column item_id of uploaded_sales table is not its primary key.
The column audience_id of sales table is not its primary key too.
I tried this one on my model, I'm not getting any errors but it still returns sales as empty:
$reports = $this->find('all',
array(
'joins' => array(
array(
'table' => 'sales',
'alias' => 'Sale',
'type' => 'left',
'conditions' => array('Sale.audience_id' => 'UploadedSale.item_id')
)),
'conditions' => array(
'UploadedSale.month' => $month,
'UploadedSale.year' => $year,
'UploadedSale.company_id' => $company_id,
'UploadedSale.item_type' => $item_type
),
'fields' => $fields
));
return $reports;
Learn about using the Containable behavior in CakePHP:
http://book.cakephp.org/view/1323/Containable
It will make joins a helluva lot easier.