How do I go around this Zend_Form_Element_Select db error? - zend-framework

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.

Related

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

Yii2:- update record using update query for mongodb

I am trying to make update query using Yii2 for mongodb. I make following query:
$collection = Yii::$app->mongodb->getCollection('usermaster');
$arrUpdate = [
'firstName' => $fname,
'lastName' => $lname,
'email' => $email,
'is_visible' => $isvisibleUser,
'phoneNumber' => $phone,
'userName' => $uname,
];
$collection->update(['_id = 55a4957sd88423d10ea7c07d'],$arrUpdate);
But it shows follwoing error in firebug.
"NetworkError: 500 Internal Server Error - http://localhost/yii2angularseedmaster/frontend/web/category/corporateupdate?corpUserid=55a4957sd88423d10ea7c07d"
My insert query works perfectly but update query does not works.
Please tell me what is wrong in my query.
$collection->update(['_id' => $id],$arrUpdate);
Your code should be like this
$collection = Yii::$app->mongodb->getCollection('usermaster');
$arrUpdate = [
'firstName' => $fname,
'lastName' => $lname,
'email' => $email,
'is_visible' => $isvisibleUser,
'phoneNumber' => $phone,
'userName' => $uname,
];
$collection->update(['_id' => $id],$arrUpdate);

Drupal custom form and autocomplete issue

Some how I managed to get it work. But still the result is not coming along with the autocomplete.
Posting my latest code now,
the textfield code
$form['town'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#autocomplete_path' => 'hfind/town/autocomplete',
);
menu function code
function hfind_menu() {
$items = array();
$items['hfind/town/autocomplete'] = array (
'title' => 'Autocomplete for cities',
'page callback' => 'hfind_town_autocomplete',
'access arguments' => array('use autocomplete'),
'type' => MENU_CALLBACK
);
return $items;
}
the callback function code
function hfind_town_autocomplete($string){
$matches = array();
$result = db_select('towns', 't')
->fields('t', array('town_name'))
->condition('town_name', '%' . db_like($string) . '%', 'LIKE')
->execute();
foreach ($result as $row) {
$matches[$row->city] = check_plain($row->city);
}
drupal_json_output($matches);
}
I hope this may the final edit.
The current situation is, autocomplete is working
The url is hfind/town/autocomplete/mtw
but it is not able to find any data from the database. I found why and unable to fix it.
It is because in the last function I've added above the $string needs to be the 'search query' but it is always querying the database as 'autocomplete'. I mean the $string variable always having the value 'autocomplete' instead of user typed value.
One more problem is, even after providing the permission to all types of user to access search autocomplete on the forms, guests users are not able to use the feature.
Please please someone help me..
`drupal_json_output()` instead of `drupal_to_js` and remove `print` .
<code>
hook_menu() {
$items['cnetusers/autocomplete'] = array(
'title' => 'Auto complete path',
'page callback' => 'cnetusers_employees_autocomplete',
'page arguments' => array(2, 3, 4, 5),
'access arguments' => array('access user profiles'),
'type' => MENU_CALLBACK,
);
return $item;
}
// my autocomplete function is like this
function cnetusers_employees_autocomplete() {
// write your sql query
$matches["$record->ename $record->elname [id: $record->uid]"] = $value;
}
if (empty($matches)) {
$matches[''] = t('No matching records found.');
}
drupal_json_output($matches);
}
$form['disc_info']['approval'] = array(
'#type' => 'textfield',
'#title' => t('Approval By'),
'#autocomplete_path' => 'cnetusers/autocomplete',
);
</code>

Zend form populate method

I have a Zend form below is the code
public function editAction()
{
try
{
$data = Zend_Auth::getInstance()->getStorage()->read();
$this->view->UserInfo = $data['UserInfo'];
$this->view->Account = $data['Account'];
$UserEditForm = $this->getUserEditForm();
$this->view->UserEditForm = $UserEditForm;
$params = $this->_request->getParams();
if ($params['user'])
{
$UserResult = $this->_user_model->getUserData($params['user']);
$UserAddressResult = $this->_user_model->getAddressData($params['user']);
$UserInfo = Array(
'UserId' => $UserResult['user_id'],
'EmailAddress' => $UserResult['email'],
'UserName' => $UserResult['username'],
'Title' => $UserResult['Title'],
'FirstName' => $UserResult['firstname'],
'LastName' => $UserResult['lastname'],
'Gender' => $UserResult['gender'],
'DateOfBirth' => date('m/d/Y', strtotime($UserResult['dateofbirth'])),
'AddressLine1' => $UserAddressResult['address_1'],
'AddressLine2' => $UserAddressResult['address_2'],
'City' => $UserAddressResult['city'],
'State' => $UserAddressResult['state_id'],
'PostalCode' => $UserAddressResult['postcode'],
'Country' => $UserAddressResult['country_id'],
'CompanyName' => $UserAddressResult['company'],
'WorkPhone' => $UserAddressResult['workphone'],
'HomePhone' => $UserAddressResult['homephone'],
'Fax' => $UserAddressResult['fax'],
'IsDashboardUser' => $UserResult['is_dashboard_user']
);
$UserEditForm->populate($UserInfo);
$this->view->UserEditForm = $UserEditForm;
}
if ($this->_request->isPost())
{
$values = $this->_request->getPost();
unset($values['Save']);
if ($UserEditForm->isValid($values))
{
$Modified_date = date('Y-m-d H:i:s');
$UserData = $this->_user_model->CheckEmail($values['EmailAddress']);
$UpdateData = $this->_user_model->UpdateUserData($UserData['user_id'], $values, $Modified_date, $data['UserId']);
if ($UpdateData != null)
{
return $this->_helper->redirector('userlist','index','user');
}
}
}
}
catch (Exception $exception)
{
echo $exception->getCode();
echo $exception->getMessage();
}
}
Because my form elements names are different then my table field names i have to declare a Array $UserAddressResult see above code to match the table field name with form element name.
Is there a another way to populate form without declaring this array.
Please don't suggest that i have to keep my table field name and form element name same. I cannot do that as per our naming convention standards.
Your naming convention is inconsistent. If it were consistent, you could perhaps use a simple regular expression to transform column names. But you can't, so you will have to do it manually one way or the other.
If you need to construct this array of values in more than one place in your code, you should consider moving the logic to the form itself (extend it with a public function populateFromUserAndResult($userResult, $userAddress)) or an action helper.
We decided to keep same name of form elements as per our table fields name. For now this is the only solutions i feel. if i come across any other solutions in future will update here...
Thanks for all your replies....

Specifying a db function in an insert statement in Zend

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