How to use findAll() in yii2? - arrays

I want to know how can i get all data of user with array id for where condition
In yii you could do something like this
$students = Student::model()->findAll("id IN ({$_POST['studentIds']})");
or
$userDtls = Student::model ()->findAllByAttributes ( array (
'id' => explode ( ",", $_POST ['studentIds'] )
) );
Now in yii2 CDbCriteria is not there, so which approach should i use to achieve same thing??
I have tried this but it only returns data for first id in the array
$result = Users::findAll([ 'id'=> $_POST ['keylist']]);
In documentation it is written that we can use this
$result = Users::findAll([1,488,489]);
But my array $_POST['keylist'] is something like this
keylist{
0='1'
1='5'
2='8'
}
I have also tried this
$ids = \Yii::$app->request->post('keylist', []);
$result = Users::findAll($ids);
And still returns data for first id in the array here is the screenshot
Thats why it doesnt work i guess
thank you

$users = Users::findAll($ids); is a correct approach.
See what you can pass in $ids in official docs here.
As I explained you here, you should never trust data from $_POST and check it for existence and validate before using.
Example of getting and check for existence with Yii2:
$ids = \Yii::$app->request->post('ids');
Or just:
$ids = isset($_POST['ids']) ? $_POST['ids'] : null;
For more complex cases I'd recommend to create separate search model and use it with validation, see Gii's CRUD for example.
UPDATE: Pay attention to what you actually pass as $ids.

$students_ids = Yii::$app->request->post('studentIds');
if(isset($students_ids)) {
$result = Users::find()->where(['in','id',$students_ids])->all();
}
var_dump($result)
Try like this

Related

Laravel : How save array in database?

I need your help because I can't save array from form in the pivot table. If someone can show me how because this is going to drive me crazy.
So I have a exam form with input number in foreach and which has a dynamic name.
And I have two total results to.
This is my data form :
I want save my input number question in pivot table and the results on another table.
For the result it's ok but it's for save in pivot table I have a problem.
Thanks to help me.
In my controller I make this :
$data = $request->all();
foreach($data as $question_id => $answer_id){
$result = new PratiqueReponse();
$result->question_id = $question_id;
$result->answer_id = $answer_id;
$result->save();
}
But not working.
You did not share much information, so i can only guess but i think you should save to db like this:
$data = $request->all();
foreach($data['questions'] as $question_id => $answer_id){
$result = new PratiqueReponse();
$result->question_id = $question_id;
$result->answer_id = $answer_id;
$result->save();
}
you need to loop trough questions, not the whole array.

How to get ID only using findBySlug method

Trying to get ID by findBySlug method in CakePHP, for example
$this->Page->findBySlug(1);
this will return all fields, but i need only ID to be returned , i have searched lot but could not find function reference as well and try following but get an Error
$this->Page->findBySlug(15, array('fields'=>array('Page.id')));
any solution within the function that doesn't use a custom query?
cakephp2
$page = $this->Page->findBySlug(1, array('id'));
$id = $page['Page']['id'];
cakephp3
$page = $this->Pages->findBySlug(1)
->select('id');
$id = $page->id;

Cakephp Paginate Find

I want to list the posts of a given user. It work but paginate is not accurate.
My code is the following
public function index($userid = null) {
if ($this->Post->exists($userid)) {
$this->set('posts',$this->Post->find('all',array('conditions'=>array('user_id'=>$userid))),
$this->paginate());
} else
{
$this->Post->recursive = 0;
$this->set('posts', $this->paginate());
}
The result give the correct list --> 3 posts, but the paginator display page number 1 and 2
Can you help me?
Thank you
Refer to the documentation
The code in the question is quite confused.
find
The find method only has two parameters:
find(string $type = 'first', array $params = array())
The third parameter (the result of calling paginate) isn't used and will be ignored - but it will setup the view variables for the pagination helper, based on the conditions used in the paginate call - there are no conditions being used.
It is not possible to paginate the result of a find call - to do so restructure the code to call paginate instead of find.
paginate
The paginate method is just a proxy for the paginator component - it can be used in several ways, this one (controller code example):
$this->paginate($conditions)
Is the most appropriate usage for the case in the question i.e. the complete action code should be similar to:
public function index($userId = null) {
$conditions = array();
if ($userId) {
$conditions['Post.user_id'] = $userId;
}
$this->set('posts',$this->paginate($conditions));
}
Note that logically, if a user id is requested that doesn't exist the response should be nothing - not everything.
I'm quite sure that conditions for paginate do now work that way.
If you want to set conditions for paginations you should do it as follows:
$this->paginate = array('conditions' => array('Post.user_id' => $userid)));
$this->set('posts', $this->paginate());
And yes, the result stored in $posts ( in view ) will be proper as you assigned proper find result to it, meanwhile you've paginated post model without any conditions whatsoever.
First off, you're checking to see if the post exists but using the $userid. Are you trying to see "if the user exists, get the posts for that user, or else get posts for ALL users"? As you have it right now, say you have the $userid = 159, but the max Post.id in your database is 28, then the condition is not being met because it is checking to see whether or not there is a Post with the id = 159 that exists, which it doesn't.
Second, your conditions are wrong. You are performing a find and then a paginate which are two separate queries. The conditions are being implemented on the find query but not the paginate but you are only displaying the find results.
public function index($userid = null) {
// setting recursive outside of if statement makes it applicable either way
$this->Post->recursive = 0;
// check if user exists
if ($this->Post->User->exists($userid)) {
// get posts for user
$this->set('posts', $this->paginate('Post', array('Post.user_id' => $userid));
}
else{
// get all posts
$this->set('posts', $this->paginate('Post'));
}
} // end index function

how would you return two groups of Array data to be accessed by JQuery AJAX?

I am learning cakePHP 1.26. and JQuery
In a Controller, I got two an action with these lines of codes :
function testing(){
$a = $this->User->findallByuser_id(1);
$b = $this->User->Post->findallByuser_id(1);
return a+b; // I was trying to return Array data $a and $b
}
how would you return two groups of Array data ($a and $b) to be accessed by JQuery AJAX?
Please help if you could.
This is essentially Nik's answer and I agree with him completely, but I think an example may be useful:
function testing(){
$a = $this->User->findallByuser_id(1);
$b = $this->User->Post->findallByuser_id(1);
return json_encode( array( 'User' => $a, 'Posts' => $b ) )
}
This is what I do and I've found it to be very effective.
I would use json_encode/json_decode and when you access it on you, while at the client side you can use $.parseJSON(json_strong) in order to convert it to object.
You need to have a relation between the user and his posts, maybe with a HasMany relation. When you have that set up, you need a query like this
$query['conditions'] = array('User.id' => $user_id);
$query['contain'] = array('Post');
$data = $this->User->find('all', $query);
See http://book.cakephp.org/view/1323/Containable for information on using Containable. Also see http://book.cakephp.org/view/1039/Associations-Linking-Models-Together for information on linking models via associations.
Hope that helps.

Need to make full names in cakePHP

If I have a person model with first_name and last_name, how do I create and display a full_name? I would like to display it at the top of my Edit and View views (i.e. "Edit Frank Luke") and other places. Simply dropping echoes to first_name and last_name isn't DRY.
I'm sorry if this is a very simple question, but nothing has yet worked.
Thank you,
Frank Luke
Edit for clarity: Okay, I have a function on the person model.
function full_name() {
return $this->Person->first_name . ' ' . $this->Person->last_name;
}
In the view, I call
echo $person['Person']['full_name']
This gives me a notice that I have an undefined index. What is the proper way to call the function from the view? Do I have to do it in the controller or elsewhere?
If what you are wanting is just to display a full name, and never need to do any database actions (comparisons, lookups), I think you should just concatenate your fields in the view.
This would be more aligned with the MVC design pattern. In your example you just want to view information in your database in a different way.
Since the action of concatenating is simple you probably don't save much code by placing it in a separate function. I think its easiest to do just in the view file.
If you want to do more fancy things ( ie Change the caps, return a link to the user ) I would recommend creating an element which you call with the Users data.
The arrays set by the save() method only return fields in the datbase, they do not call model functions. To properly use the function above (located in your model), you will need to add the following:
to the controller, in the $action method:
$this->set( 'fullname', $this->Person->full_name();
// must have $this-Person->id set, or redefine the method to include $person_id
in the view,
echo $fullname;
Basically, you need to use the controller to gather the data from the model, and assign it to the controller. It's the same process as you have before, where you assign the returned data from the find() call to the variable in the view, except youre getting the data from a different source.
There are multiple ways of doing this. One way is to use the afterFind-function in a model-class.
See: http://book.cakephp.org/view/681/afterFind.
BUT, this function does not handle nested data very well, instead, it doesn't handles it al all!
Therefore I use the afterfind-function in the app_model that walks through the resultset
function afterFind($results, $primary=false){
$name = isset($this->alias) ? $this->alias : $this->name;
// see if the model wants to attach attributes
if (method_exists($this, '_attachAttributes')){
// check if array is not multidimensional by checking the key 'id'
if (isset($results['id'])) {
$results = $this->_attachAttributes($results);
} else {
// we want each row to have an array of required attributes
for ($i = 0; $i < sizeof($results); $i++) {
// check if this is a model, or if it is an array of models
if (isset($results[$i][$name]) ){
// this is the model we want, see if it's a single or array
if (isset($results[$i][$name][0]) ){
// run on every model
for ($j = 0; $j < sizeof($results[$i][$name]); $j++) {
$results[$i][$name][$j] = $this->_attachAttributes($results[$i][$name][$j]);
}
} else {
$results[$i][$name] = $this->_attachAttributes($results[$i][$name]);
}
} else {
if (isset($results[$i]['id'])) {
$results[$i] = $this->_attachAttributes($results[$i]);
}
}
}
}
}
return $results;
}
And then I add a _attachAttributes-function in the model-class, for e.g. in your Person.php
function _attachAttributes($data) {
if (isset($data['first_name']) && isset($data['last_name'])) {
$data['full_name'] = sprintf("%s %s %s", $data['first_name'], $data['last_name']);
}
return $data;
}
This method can handle nested modelData, for e.g. Person hasMany Posts then this method can also attachAttributes inside the Post-model.
This method also keeps in mind that the linked models with other names than the className are fixed, because of the use of the alias and not only the name (which is the className).
You must use afterFind callback for it.
You would probably need to take the two fields that are returned from your database and concatenate them into one string variable that can then be displayed.
http://old.nabble.com/Problems-with-CONCAT-function-td22640199.html
http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/
Read the first one to find out how to use the 'fields' key i.e. find( 'all', array( 'fields' => array( )) to pass a CONCAT to the CakePHP query builder.
The second link shows you how to merge the numeric indexes that get returned when you use custom fields back into the appropriate location in the returned results.
This should of course be placed in a model function and called from there.

Resources