Laravel Jenssegers insert array values - mongodb

I am using Laravel with mongodb(Jenssegers), i have array data like following,
$insert[] = ['sub_id'=>$loggedin,
'userid' => $row->userid,
'username' => $row->username,
'email' => $row->email,
'mobileno' => $row->mobileno,
'manager_mail' => $row->manager_mail,
'roleid' => $userrole->roleid];
my insert query is,
$user->save($insert);
But its not working, please suggest any solution?

You need to use create() or insert() methods:
User::create($insert);
Or:
User::insert($insert);
Or:
DB::table('table_name')->insert($insert);

Eloquent save method accept 1D array. But you provided 2D. Please try following code:
$insert = ['sub_id'=>$loggedin,
'userid' => $row->userid,
'username' => $row->username,
'email' => $row->email,
'mobileno' => $row->mobileno,
'manager_mail' => $row->manager_mail,
'roleid' => $userrole->roleid];
$user->save($insert);

Related

Insert data with data type in laravel?

I am using laravel with mongodb, This is my update query
$user = User::where('_id', '=', $row)
->update(['userid' => $userid,
'username' => $username,
'email' => $emailid,
'mobileno' => $mobile,
'manager_mail' => $manager_mail,
'roleid' => $role]);
While inserting role id is inserted as a string, how can i insert as a integer? please help
If you know $roleid will always be a numeric value, you can use intval in php when passing $roleid.
E.g. intval($role)
Read about it here: http://php.net/manual/en/function.intval.php

Mailpoet Custom Forms and Custom Fields

I've created a custom mailpoet form and am trying to add subscribers through it. It works when I don't try to post custom form field data. Does anyone know how I can make it work with custom form fields? In the database it has cf_1, cf_2 etc and this is what I've tried amending the code here without much luck: https://support.mailpoet.com/knowledgebase/plugin-form-integrate/
$user_data = array(
'email' => $email,
'firstname' => $firstname,
'lastname' => $lastname,
'cf_1' => $phone,
'cf_2' => $company_name,
'cf_3' => $dob,
);
Any ideas? Cheers!
I trawled through some of the plugin's code and figured out how to do it. So based on the example given by the plugin authors I've made this change:
//in this array firstname and lastname are optional
$user_data = array(
'email' => $email,
'firstname' => $firstname,
'lastname' => $lastname
);
$custom_data = array(
'cf_1' => $phone,
);
$data_subscriber = array(
'user' => $user_data,
'user_field' => $custom_data,
'user_list' => array('list_ids' => array(3))
);
$helper_user = WYSIJA::get('user','helper');
$helper_user->addSubscriber($data_subscriber);
So pretty much in the data_subscriber multidimensional array you need to add an another field for "user_field" and that should be an array, see the $custom_data array I created.
Tested and works, hope that helps someone else!

MongoDb double find on update query

I finded a data my mongodb database. I want be update a array's field this data.
My data is here:
http://paste.ubuntu.com/8447715/
I want will find this data and update home adress. I am trying:
$Data = array(
'$set' => array(
'address.name' => 'home'
)
); <br>
$users->update(array('username' => 'micheal', 'address.name' => 'hame') ,$Data);
What's wrong ?
My english is bad,sorry
You need to use the $ operator to update the address that matches the selection;
$Data = array(
'$set' => array(
'address.$.name' => 'home'
)
);
$users->update(array('username' => 'micheal', 'address.name' => 'hame') ,$Data);

How to use the "LIKE" statement in mongodb cakephp

I am using ICHIKAWAY's mongodb driver for cakephp.
One thing that I don't really get is how to perform a "LIKE" statement in cakephp using MONGODB.
I tried this statement:
$users = $this->User->find('all', array('conditions' => array('data_type' => 'user', 'profile.firstname LIKE' => '%'.$string)));
but its not working since "LIKE" is an mysql function.
Thanks for the suggestions.
Use MongoRegex
Mongo DB has a LIKE operator - it's simply a regular expression, to use it:
$users = $this->User->find('all', array(
'conditions' => array(
'data_type' => 'user',
'profile.firstname' => new MongoRegex("/$string/i")
)
));
There's an SQL Compatibilty behavior
The Mongodb driver contains a behavior providing sql syntax compatibility. To use it, simply make sure your model is using this behavior:
class User extends AppModel {
$actsAs = array(
'MongoDB.SqlCompatible'
)
}
And then you can use your query exactly as it appears in the question:
$users = $this->User->find('all', array(
'conditions' => array(
'data_type' => 'user',
'profile.firstname LIKE' => '%'.$string
)
));

Zend_Validate_Db_RecordExists against 2 fields

I usualy use Zend_Validate_Db_RecordExists to update or insert a record. This works fine with one field to check against. How to do it if you have two fields to check?
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector,day_of_week'
)
);
if ($validator->isValid($fields_values['id_sector'],$fields_values['day_of_week'])){
//true
}
I tried it with an array and comma separated list, nothing works... Any help is welcome.
Regards
Andrea
To do this you would have to extend the Zend_Validate_Db_RecordExists class.
It doesn't currently know how to check for the existence of more than one field.
You could just use two different validator instances to check the two fields separately. This is the only work around that I can see right now besides extending it.
If you choose to extend it then you'll have to find some way of passing in all the fields to the constructor ( array seems like a good choice ), and then you'll have to dig into the method that creates the sql query. In this method you'll have to loop over the array of fields that were passed in to the constructor.
You should look into using the exclude parameter. Something like this should do what you want:
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => $this->_name,
'field' => 'id_sector',
'exclude' => array(
'field' => 'day_of_week',
'value' => $fields_values['day_of_week']
)
);
The exclude field will effectively add to the automatically generated WHERE part to create something equivalent to this:
WHERE `id_sector` = $fields_values['id_sector'] AND `day_of_week` = $fields_values['day_of_week']
Its kind of a hack in that we're using it for the opposite of what it was intended, but its working for me similar to this (I'm using it with Db_NoRecordExists).
Source: Zend_Validate_Db_NoRecordExists example
Sorry for the late reply.
The best option that worked for me is this:
// create an instance of the Zend_Validate_Db_RecordExists class
// pass in the database table name and the first field (as usual)...
$validator = new Zend_Validate_Db_RecordExists(array(
'table' => 'tablename',
'field' => 'first_field'
));
// reset the where clause used by Zend_Validate_Db_RecordExists
$validator->getSelect()->reset('where');
// set again the first field and the second field.
// :value is a named parameter that will be substituted
// by the value passed to the isValid method
$validator->getSelect()->where('first_field = ?', $first_field);
$validator->getSelect()->where('second_field = :value', $second_field);
// add your new record exist based on 2 fields validator to your element.
$element = new Zend_Form_Element_Text('element');
$element->addValidator($validator);
// add the validated element to the form.
$form->addElement($element);
I hope that will help someone :)
Although, I would strongly recommend a neater solution which would be to extend the Zend_Validate_Db_RecordExists class with the above code.
Enjoy!!
Rosario
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
'validators' => array('EmailAddress', $obj= new Zend_Validate_Db_NoRecordExists(array('adapter'=>$dbAdapter,
'field'=>'email',
'table'=>'user',
'exclude'=>array('field'=>'email','value'=>$this->_options['email'], 'field'=>'is_deleted', 'value'=>'1')
))),
For those using Zend 2, If you want to check if user with given id and email exists in table users, It is possible this way.
First, you create the select object that will be use as parameter for the Zend\Validator\Db\RecordExists object
$select = new Zend\Db\Sql\Select();
$select->from('users')
->where->equalTo('id', $user_id)
->where->equalTo('email', $email);
Now, create RecordExists object and check the existence this way
$validator = new Zend\Validator\Db\RecordExists($select);
$validator->setAdapter($dbAdapter);
if ($validator->isValid($username)) {
echo 'This user is valid';
} else {
//get and display errors
$messages = $validator->getMessages();
foreach ($messages as $message) {
echo "$message\n";
}
}
This sample is from ZF2 official doc
You can use the 'exclude' in this parameter pass the second clause that you want to filter through.
$clause = 'table.field2 = value';
$validator = new Zend_Validate_Db_RecordExists(
array(
'table' => 'table',
'field' => 'field1',
'exclude' => $clause
)
);
if ($validator->isValid('value') {
true;
}
I am using zend framework v.3 and validation via InputFilter(), it uses same validation rules as zend framework 2.
In my case I need to check, if location exists in db (by 'id' field) and has needed company's id ('company_id' field).
I implemented it in next way:
$clause = new Operator('company_id', Operator::OP_EQ, $companyId);
$inputFilter->add([
'name' => 'location_id',
'required' => false,
'filters' => [
['name' => 'StringTrim'],
['name' => 'ToInt'],
],
'validators' => [
[
'name' => 'Int',
],
[
'name' => 'dbRecordExists',
'options' => [
'adapter' => $dbAdapterCore,
'table' => 'locations',
'field' => 'id',
'exclude' => $clause,
'messages' => [
'noRecordFound' => "Location does not exist.",
],
]
],
],
]);
In this case validation will pass, only if 'locations' table has item with columns id == $value and company_id == $companyId, like next:
select * from location where id = ? AND company_id = ?