Specifying a db function in an insert statement in Zend - zend-framework

I have something like this:
$data = array('username' => 'John', 'password' => 'john123');
$table = new Zend_Db_Table('users');
$table->insert($data);
But I want it to include a hashing function to the password, so the sql is something like this:
INSERT INTO `users` (`username`, `password`) VALUES ('John', PASSWORD('john123'));
How can I do that, elegantly?

$data = array('username' => 'John', 'password' => new Zend_Db_Expr('SHA1(john123)');

Related

How to select a calculated value including parameter using Zend FW?

I'm using Zend FW and have a select with specified columns to be selected, e.g.
$select = $this->_db->select();
$select->from( array("t" => 'table'), array( 'id', 'name', 'some_number'));
Now I want to get the some_number value multiplied by some given parameter, like :
select id, name, (some_number * $param) from table;
How to do that?
Got it, using Zend_Db_Expr. If someone else needs... That works for me:
$select->from( array("t" => 'table'),
array( 'id', 'name', 'new_number' => new Zend_Db_Expr('some_number * ' . $param ) )
);

Laravel Jenssegers insert array values

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);

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

Cakephp table query

I have a members table. It has these columns:
ID
fname
lname
parent
spouse
I want 'parent' field to have select option in a form which uses 'fname' & 'lname' and store it in 'parent' field. I have tried to do this Members controller:
$results = $this->Members->find('list',
array('order'=>array('fname DESC')));
$parent = $results->toArray();
I am calling $parent variable as an input. However that method doesn't work. I don't know how to figure it out.
When calling list you can configure the fields used for the key and value with the keyField and valueField options respectively.
Doc: https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#finding-key-value-pairs
Like this
$query = $articles->find('list', [
'keyField' => 'slug',
'valueField' => 'title'
]);
In your case it will be like this
$results = $this->Members->find('list', array(
'keyField' => 'ID',
'valueField' => 'parent'
'order'=>array('fname DESC'),
));
$parent = $results->toArray();

How do I go around this Zend_Form_Element_Select db error?

When i use the Zend_Form_Element_Select elements with multioptions i get this error when i pass the selected value to Zend_DB_Table to insert into the db
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'accounts_status ' in 'field list'
I have extracted some code snippets that i believe will go a long way into illustrating my problem.The accounts_status field DOES exist in my table
On my form construct have added the select element and options (I have left out the other elements)
$optionsstatus = array(
'active' => 'active',
'pending' => 'pending'
);
$optionsrole = array(
'guest' => 'guest',
'user' => 'user',
'writer' => 'writer',
'admin' => 'admin'
);
$status = new Zend_Form_Element_Select('accounts_status');
$status->setLabel('Status')
->setRequired(true)
->addMultiOptions($optionsstatus);
$role = new Zend_Form_Element_Select('accounts_role');
$role->setLabel('Role')
->setRequired(true)
->addMultiOptions($optionsrole);
I use the Zend_DB_table to insert the post values from my controller
public function addaccount($username, $fullname, $email,
$password,$status,$roles,$comments)
{
$data = array(
'accounts_username' => $username,
'accounts_fullname' => $fullname,
'accounts_email' => $email,
'accounts_password' => $password,
'accounts_status ' => $status,
'accounts_roles' => $roles,
'accounts_comments ' => $comments,
);
$this->insert($data);
}
In my controller i get the post values and send them to my model
$username = $form->getValue('accounts_username');
$fullname = $form->getValue('accounts_fullname');
$email = $form->getValue('accounts_email');
$password = $form->getValue('accounts_password');
$status = $form->getValue('accounts_status');
$roles = $form->getValue('accounts_roles');
$comments = $form->getValue('accounts_comments');
$accounts = new Model_DbTable_Account();
$accounts->addaccount($username, $fullname,$email,
$password,$status,$roles,$comments);
This approach works for me except when am dealing with the Zend_Form_Element_Select elements.Am just wondering if there is a specific way of dealing with this select elements when it comes to CRUD operations.
Message: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'accounts_status ' in 'field list'
Maybe I'm crazy, but looks to me like there's an extra space on that end of that 'accounts_status ' field name.