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.
Related
I have been looking for a solution that would replicate MySQL's INSERT ... ON DUPLICATE KEY UPDATE in my Codeigniter model, which led me to the save() function.
I get the logic of it, and my script works fine... but for UPDATING only.
What I've realised now is that my data array is formed from info pulled from an external datafeed, and includes both new AND existing products. There will ALWAYS be a sku (primary key) included in for every product, therefore the save function will ALWAYS look to update the corresponding row in my database - but never insert it - hence, no new objects are ever added to my database - only updated.
// Defined as a model property
$primaryKey = 'sku';
// Does an insert()
$data = [
'brand' => 'Heinz',
'name' => 'Baked Beans'
];
$userModel->save($data);
// Performs an update, since the primary key, 'sku', is found.
$data = [
'sku' => xrt567ycr,
'brand' => 'Heinz',
'title' => 'Baked Beans'
];
$userModel->save($data);
What is the easiest way to get around this and have the script either insert or update based on whether the sku is actually present in the database already or not? All help greatly appreciated!
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);');
I use MySQL using InnoDB tables with CodeIgniter Datamapper in my PHP application. Often, the user is given the option of deleting a record through the app by initiating a ->delete function call. When a record has child records (one-to-one or one-to-many), I would also like these records to be deleted along with the parent record, if it is stated by FK constraints in the database.
In this case, I have 2 tables, items and input_lines. I have confirmed that both are using InnoDB. Each item can have many input_lines, so input_lines has a field called item_id, which is set to NULL, indexed, and have FK constraints (ON CASCADE DELETE and ON CASCADE UPDATE). I have set the config element in the DM config file as
$config['cascade_delete'] = FALSE
Because in the documentation it says you should do that if you are using ON UPDATE/DELETE CASCADE. However, when the user initiates the $item->delete() method, only the item is deleted, and the item_id fields on the input_line records associated with the item are set to null.
My models look like this:
class Item extends DataMapper {
public $has_many = array('labour', 'item_type', 'input_line', 'custom_item_type');
...
}
class Input_line extends DataMapper {
public $has_one = array('item');
...
}
I have tried this with cascade_delete = false and true and it won't work. I know the constraints work because deleting the record with MySQL directly works as expected, deleting the child records.
What am I missing? Why is it setting the FK fields to null instead of deleting the record?
EDIT 1:
I decided against my better judgment to debug the delete function in datamapper.php (libraries directory).
I noticed this code in that function:
// Delete all "has many" and "has one" relations for this object first
foreach (array('has_many', 'has_one') as $type)
{
foreach ($this->{$type} as $model => $properties)
{
// do we want cascading delete's?
if ($properties['cascade_delete'])
{
....
So I var_dumped the contents of $properties, and I saw this:
array (size=8)
'class' => string 'labour' (length=6)
'other_field' => string 'item' (length=4)
'join_self_as' => string 'item' (length=4)
'join_other_as' => string 'labour' (length=6)
'join_table' => string '' (length=0)
'reciprocal' => boolean false
'auto_populate' => null
'cascade_delete' => boolean true
It appears the default for when the model doesn't have the property specifically initialized is overriding the config value. This seems like too glaring a mistake so there's definitely something I'm doing wrong somewhere...right? I really, really want to avoid hacking the DM core files...
EDIT 2:
I was thinking maybe the config file wasn't being found, but I checked the logs and there're entries stating that the Datamapper config file was successfully loaded, so that's not the issue.
Doesn't look like anyone has any answers.
My solution was to change the property in the datamapper library $cascade_delete to false, since it's set to true right now. It's unfortunate that I have to resort to hacking the core, but DM won't respect my changes in the config file for cascade_delete so I have no other choice.
If anyone comes across this question and has encountered an issue like this before, please comment.
I have come across the same problem, but finally I just did like this:
$sql = "DELETE FROM EVENT WHERE event_id=".$event_id.";";
$this->db->query ($sql );
In case we set "ON DELETE CASCADE" for the foreign key which refers to event_id, the above SQL works fine, so I just call it directly.
I am using a mongodb-codeigniter library by Alex Bilbie.
I have a collection called "user_visits". I insert into collection like below.
$query = $this->mongo_db->insert('user_restaurant_visits',[
'_id' => 1,
'user_id' => $user_id,
'pages_visited' => [
'page_id' => $restaurant_id
'visited' => [
'deal' => $purchase_deal,
'ordered' => $purchased_item
]
]
]);
Now all I would want to know is,
I would like to add the documents by "upsert=true" boolean flag as specified in here which will insert if the field is not present and update if the field is present. And I could not find a way to do so in the library I use! Am I misguided?
Is this a good way? is there anything wrong in the way I have organized the fields (I mean Schema as we say in RDBMS). I specifically ask this because, some feel nested arrays are better than embedded documents. like philnate says here in his answer and comments
If I'd want to upsert, increment a field, and addToSet, in the same query, is this possible with the library I currently use?
Let me know if I miss something, I can clarify in comments. I am totally new to NoSQL DBs.
I am sorry if that looked amaeturish.
Answer of one of your question :
How to Use "Upsert" in CodeIgniter while updating :
// Where Condition, if any
$this->mongo_db->where(array('condition_key' => 'condition_value'));
// Update Data Array
$this->mongo_db->set($mongoArray);
// Set Options
$option = array('upsert' => true);
// Call Update Function
$this->mongo_db->update('Collection_Name', $option);
I hope this will help you :)
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)