Save the user_id of a logged in user when submitting a form in ZF2 - zend-framework

I want to save the users user_id along the data he submits via a form. This is in my controller:
The controller:
$request = $this->getRequest();
if ($request->isPost()) {
$testing = new Testing();
$form->setInputFilter($testing->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$this->zfcUserAuthentication()->hasIdentity(); // user logged in?
$user = $this->zfcUserAuthentication()->getIdentity(); //get identity
$user_id = $user->getID(); //gets the user-id, output: number, eg. 18
$testing->exchangeArray($form->getData());
$this->getTestingTable()->saveTesting($testing);
// Redirect to iaps test
return $this->redirect()->toUrl('/testing/iaps4');
}
}
return array('form' => $form);
}
If I print_r($user_id) the output will be plain and simple 18.
I tried several things that did not work, here the most promising ones:
a seperate exchangeArray for the user_id only. Unfortunately, it
only generates an empty entry in my database along the data form.
combine the $testing->exchangeArray($form->getData()); with the
user_id data, obviously no success here either.
The thing is, I kinda know what I need: the plain output 18 from the $user->getID(); won't help me, because I need to asign this number to the user_id column in the database along the data submitted by the user with the id user_id.
I'm using Zend Framework 2.3.3, ZF-Commons/ZfcBase 0.0.1 and ZF-Commons/ZfcUser 1.2.2

You can merge user_id with the data from your form in a single array with array_merge :
$testing->exchangeArray(array_merge(
$form->getData(),
['user_id' => $user->getID()]
));

Related

CakePHP 3 - Exclude fields by default in query unless specifically selected

Title pretty much says it all. I have some tables with fields that contain a lot of data. To save some performance I would like to not SELECT these by default.
The emphasis on the new default behaviour, differentiating the question from e.g. Select all except one field in cakephp 3 query
Example:
$cities = $this->Cities->find();
// A $city does not include the field `shape` (which is a huge polygon)
$cities = $this->Cities->find(['id', 'name', 'shape']);
// A $city now does include the `shape` property
I looked at the accessible and hidden properties of an entity, but these don't seem to affect the SELECT statement.
EDIT: The selectAllExcept query seems usefull. I combined this with the beforeFilter event like this:
public function beforeFind($event, $query, $options, $primary)
{
$query->selectAllExcept($this, ['shape']);
}
This works well for empty queries, shape is now excluded. But now I have no control over the other fields that might want to include or not:
$this->Cities->find()->select(['id', 'shape']) will then also select the other fields because the selectAllExcept().
You can simple overwrite find('all') method in your table.
For example in UsersTable:
public function findAll(Query $query, array $options)
{
$query->selectAllExcept($this, ['password']);
return $query;
}
then in your controller:
// select all except password
$users = $this->Users->find();
debug($users);
OR
// we try to select some fields, without success
$users = $this->Users->find()->select(['id', 'username', 'password']);
debug($users);
OR
// we try to select some fields incl. password, with success
$users = $this->Users->find()->select(['id', 'username', 'password'], true); // <-- this overwrite select / selectAllExcept in custom finder
debug($users);

Magento insert and filter multi-select values in custom admin-grid module

I have created a custom module with Module Creator (v 1.7).
Have one multi-select admin form field.
As the multiselect field on submitting gives array , the same value (i.e Array) is stored in database.
To avoid this on saving the submitted value , I just manipulated the code by storing it in e.g a,b,c form.
Through this the data is saved successfully.
Now in grid , I want to filter it , as we have for Status part [1=>Enabled 2=>Disabled].
please suggest How would I achieve this .
Take a look at magento filter_condition_callback option
$this->addColumn('categories', array(
....
'filter_condition_callback' => array($this, '_applyMyFilter'),
..
)
);
protected function _filterCategoriesCondition($collection, $column)
{
if (!$value = $column->getFilter()->getValue()) {
return;
}
$this->getCollection()->addFieldToFilter('categories', array('finset' => $value));
}
See
Filtering a text-type column with MySQL-computed values in Magento Admin Grid
Magento: How to search or filter by multiselect attribute in admin grid?

required field for other model's field in yii

I need to store a record in First Model. It has around 10 fields. Now, I need to apply required rule for one field which i'm storing in SECOND model and I'm getting these values for this field from Third model.(it is a drop down field)
how can i make this field mandatory in yii??
can any one help please..
Many Thanks,
You can add additional attributes and rules to a model. You don't have to only use attributes that directly relate to fields in your database table. Let's look at a basic example. You have a user (User) table which has the following fields:
id
email
password
And you have another table which stores user profile (UserProfile) information which has the structure:
id
user_id
country
When the user creates their account, you would have a form that captures their information. Your User model would have rules like:
array('email, password', 'required'),
array('email', 'unique', 'message'=>'Email already in use'),
...
You can add an attribute for country to your User model like so:
class User extends CActiveRecord {
public $country;
...
And then in your rules you can add the new attribute:
array('email, password, country', 'required'),
array('email', 'unique', 'message'=>'Email already in use'),
...
The country attribute will now be a part of your User model. You can now add that to your form:
<?php echo $form->dropDownList($model,'country',CHtml::listData(Country::model()->findAll(),'id','country_name'),array('empty'=>'-- select a country --')); ?>
Now on your form submit, the $model->validate() method will validate the country field. You can save this manually to your second model (UserProfile), something like:
if(isset($_POST['User'])){
if ($model->validate()){
$user_profile = new UserProfile;
$user_profile->user_id = $model->id;
$user_profile->country = $model->country;
$user_profile->save();
...
Hopefully that answers your question.

Selecting multiple checking boxes with FormHelper in CakePHP

So in an Index view for a certain model, I'm including some checkboxes; I have an array of days of the week, and so have 7 checkboxes available, per record.
I can save data, no problem. What I can't figure out is how to pre-select the checkboxes, based on each record's saved data. Currently I'm having to do this:
<?php if ( isset($user['SurveyAssignment'][0]['active_days']['Monday']) && $user['SurveyAssignment'][0]['active_days']['Monday'] == 1 ) { $monChecked = true; } else { $monChecked = false; } ?>
<?php echo $this->Form->input('SurveyAssignment.' . $count .'.active_days.Monday', array('type' => 'checkbox', 'label' => false, 'div' => false, 'checked' => $monChecked));?>
And that's just for ONE day, for one record. Right now, I'm doing that 7 times, for each record. It's very bloated.
I would have thought I could just read in the array for each record, and select a group of checkboxes accordingly.
I'm sure I'm missing something very simple but I can't see past my nose at the moment.
If your associations are set up correctly, your field name is correct, and you're passing the correct data, they will be selected automatically for you.
The likely answer is you're not using the correct field or Model.field for your form input.

facebook user_id stores strange numbers

i´m developing a Facebook Fanpage using PHP-SDK 3.0 and integrated a Facebook Registration Plugin. So far everthing works fine. I can store everthing.
The signed_request answered:
signed_request contents:
Array (
[algorithm] => HMAC-SHA256
[expires] => 1324xxxx400
[issued_at] => 132446xxx80
[oauth_token] => AAADRjT73VhwBALl6Gb3EVarvyGU7xxxxxxxxxxxxxxxxxxxSAUuoZAGlydkX2pH3
[registration] => Array ()
[user_id] => 10xxxxxxx5426 )
For storing the data I use the following code:
$email = $response["registration"]["email"];
$user_fbid = $response["registration"]["user_id"];
$email = mysql_real_escape_string($email);
$user_fbid = mysql_real_escape_string($user_fbid);
// Inserting into users table
$result = mysql_query("INSERT INTO member (email, userid) VALUES ($email, $user_fbid)
Here my question:
At first day the $result stored something like 214xxxx647 and next day it stores 217. But there is nothing similar like 10xxxxxxx5426
I think your problem is you are storing the user_id value into a "int" which means the maximum value that can be stored is 2147483647.
Facebook user_id's require a larger pool of numbers, therefore your should change the column datatype to BIGINT.