add indexing to database using codeigniter migration? - php

How can i add index to any field in database using codeigniter migration.?
I have tried with alter query but i didn't find array key for indexing to pass in alter query?
$fields = array('photo_id' =>
array('type' => 'INT',
'constraint'=>11,
'after'=>'photo_req_id',`enter code here`
'null'=>false));
And i also try to find on codeigniter documentation but not mention anything about indexing.
Guys please help to set indexing by using codeigniter migration method.

Indexing is done by using the add_key function documented here.
To create a PRIMARY KEY index on photo_id
$this->dbforge->add_key('photo_id', TRUE);
To create a secondary index on photo_id
$this->dbforge->add_key('photo_id');

There is no such interface until CI 3 and the under development CI 4.
You can use
$this->db->query('CREATE INDEX your_index_name ON your_table_name (your_column_name);');

Related

How to create a new row based on custom value ID in laravel 4.2

Am having a table with id as primary key not auto increment, I need to push the custom value as an ID. I tried with firstOrNew() and findOrCreate() and create() by pushing the ID as an argument like
$var_id = '99';
$var = Table::findOrCreate(['id'=>$var_id]);
return $var;
But its creating new row with 0 as ID. Can any one please suggest me how to create ID with 99.
Thanks
Did you create your table manually or through a migration? Make sure your id is set to "auto-increment" on your table. If you create through a migration it would look something like this $table->increments('id');
Try laravel query builder its flexible to use.
DB::table('tablename')->insert(
array('id' => '99'));
For more help check this link out
Laravel 4.2 query builder
I guess you're code has a small typo, in your case you probably want to use firstOrCreate or findOrNew.
Either way, by default all model attributes are guarded.
Add id to your model fillable array, like so:
protected $fillable = ['id']; // perhaps more ?
And try again.

How to create autoincrement field in Mandango

I'm using in my project mandago ODM for mongodb.
http://mandango.org
I know that in MongoDb you can define JS functions on fields but I don't know how to do it with mandango. I create autoincrement ID field in more clever way than getting last record then incrementing it in PHP and saving in db. So my question is how to create an autoincrement field in mandago ODM?
I'd put some code but there's really nothing to put just pure code classes generated by Mondator.
After some research I have found out how to solve problem.
You need to add in your model mapping file 'idGenerator' => 'sequence'
in my case it looks as following:
$modelMapping = array(
'Model\User' => array(
'isEmbedded' => false,
'idGenerator' => 'sequence',
...
It will autoincrement _ID key in your document.

How to create table in yii with use createcommand or other?

how to create a table using createCommand in Yii?
I've followed the instructions in the http://www.yiiframework.com/doc/api/1.1/CDbCommand but I am still confused. Can you give an example of creating a table using createCommand in yii.
thank you
Assuming you've named your database connection string db (in protected/config/main.php) as follows,
'components'=>array(
'db'=>array(
),
),
you should be able to create a table using the following command:
$sqlQuery = "CREATE TABLE IF NOT EXISTS pet (name VARCHAR(20), owner VARCHAR(20))";
$sqlCommand = Yii::app()->db->createCommand($sqlQuery);
$sqlCommand->execute();
You should also be able to replace the first line with any SQL statement and execute it successfully. If you want to query for data, you will need to use queryRow, queryColumn, or queryScalar (as defined in the documentation).
Hope this helps!
I use this code and sucess..
Yii::app()->db->createCommand("CREATE TABLE {$nama_data}(id serial, {$list_field}, x text,y text,wkt text, the_geom geometry,PRIMARY KEY(id));")->query();

Manually packaging data for CakePHP's save()

I'm trying to package up some data for the save() function in cakephp. I'm new to PHP, so I'm confused about how to actually write the below in code:
Array
(
[ModelName] => Array
(
[fieldname1] => 'value'
[fieldname2] => 'value'
)
)
Thank you!
To answer your question, you can create the array structure you need, and save it, by doing this:
<?php
$data = array(
'ModelName' => array(
'fieldname1' => 'value',
'fieldname2' => 'value'
)
);
$this->ModelName->save($data);
?>
Please note:
Based on what you've written above in your comments it looks like you're not keeping to the CakePHP conventions. It's possible to do things this way but you'll save yourself a lot of time and trouble if you decided to stick with the CakePHP defaults as much as possible, and only do it your own way when you have a good reason to.
A couple things to remember are:
Model names should be singular. This means that your model should be called Follower instead of Followers.
The model's primary key in the database should be named just id, not followers_id, and should be set as PRIMARY KEY and AUTO_INCREMENT in your database.
If you decide not to follow the conventions you'll probably find yourself scratching your head, wondering why things aren't working, every step of the way. Try having a look at the CakePHP documentation for more details.
I think you need to do like below:
$this->Followers->create();
$this->data['Followers']['user_id'] = $user_id;
$this->data['Followers']['follower_id'] = $follower_id; // If it is primary and auto increment than you don't need this line.
$this->Followers->save($this->data)

Using Multiple Tables in a Zend Model and returning a combined result set

Hi This is either a very specific or very generic quetion - I'm not sure, and I'm new to the Zend framework / oo generally. Please be patient if this is a stupid Q...
Anyway, I want to create a model which does something like:
Read all the itmes from a table 'gifts' into a row set
for each row in the table, read from a second table which shows how many have been bought, the append this as another "field" in the returned row
return the row set, with the number bought included.
Most of the simple Zend examples seem to only use one table in a model, but my reading seems to suggest that I should do most of the work there, rather than in the controller. If this is too generic a question, any example of a model that works with 2 tables and returns an array would be great!
thanks for your help in advance!
I assume second tables is something like "gift_order" or something.
In this case you need to specify tables relationships beetween "gift" and and "gift_order" via foreign keys and describe it in table class.
It will look like this
class GiftOrder extends Zend_Db_Table_Abstract
{
/** Table name */
protected $_name = 'gif_order';
protected $_referenceMap = array(
"Fileset" =>array(
"columns" => array("gifId"),
"refTableClass" => "Gift",
"refColumns" => array("id")
));
........................
You need to specify foreigh key constraint while create table with SQL
ALTER TABLE `gift_order`
ADD CONSTRAINT `order_to_gift` FOREIGN KEY (`giftId`) REFERENCES `gift` (`id`) ON DELETE CASCADE;
If this is something you looking for you could find more on this at this link link http://framework.zend.com/manual/en/zend.db.table.relationships.html
With such solution you will be able to loop gifts and get their orders without any complex SQL's
$rowSetGifts = $this->findGifts();
while($rowSetGifts->next()){
$gift = $rowSetGifts->current();
$orders = $gift->findGiftOrder();//This is magick methods, this is the same $gift->findDependentRowset('GiftOrder');
//Now you can do something with orders - count($orders), loop them or edit
}
I would recommend creating a function in your gifts model class that returns what you want. It would probably look something like:
public function getGiftWithAdditionalField($giftId) {
$select = $this->getAdapter()->select()
->from(array('g' => 'gifts'))
->joinLeft(array('table2' => 't2'), 'g.gift_id = t2.gift_id', array('field' => 'field'))
->where('g.gift_id = ?', $giftId);
return $this->getAdapter->fetchAll($select);
}
You can check out the Zend Framework Docs on Joins for more info.

Categories