I have an array as follows:
[
0 => [
'name' => 'CARD'
'id' => '0'
]
1 => [
'name' => 'MOBILE'
'id' => '1'
]
2 => [
'name' => 'GIFT'
'id' => '2'
]
]
I want to change the key id to type in all the array. Is there a way to do this in Yii2 using ArrayHelper?
You can use getColumn() for this:
$result = ArrayHelper::getColumn($array, function ($data) {
return [
'name' => $data['name'],
'type' => $data['id'],
];
});
But it will not really differ from array_map() or simple foreach.
There is not an array helper for this but you could do this with a php foreach
foreach ($myArray as $key => $value) {
$myArray[$key]['type'] = $value['id'];
unset($myArray[$key]['id']);
}
Related
I'm doing cakephp4 controller test with phpUnit but when I call save my id auto_increment disapear.
My table before save():
Image before save
The test:
public function testAdd(): void
{
$this->session([
'Auth' => [
'id' => 1,
'DNI_CIF' => '22175395Z',
'name' => 'Prueba',
'lastname' => 'Prueba Prueba',
'username' => 'Pruebatesting',
'password' => 'prueba',
'email' => 'prueba#gmail.com',
'phone' => '639087621',
'role' => 'admin',
'addres_id' => 1
]
]);
$this->get('animal/add');
$this->assertResponseOk();
$data=[
'id' => 1,
'name' => 'AñadirAnimal',
'image' => '',
'specie' => 'dog',
'chip' => 'no',
'sex' => 'intact_male',
'race' => 'cat',
'age' => 1,
'information' => 'Es un animal.',
'state' => 'sick',
'animal_shelter' => [
'id' => 1,
'start_date' => '2022-11-03 10:47:38',
'end_date' => '2022-11-03 10:47:38',
'user_id' => 1,
'animal_id' => 1
]
];
$this->enableCsrfToken();
$this->post('animal/add',$data);
$this->assertResponseOk();
}
The controller:
public function add()
{
$animal = $this->Animal->newEmptyEntity();
if ($this->request->is('post')) {
$animal = $this->Animal->patchEntity($animal, $this->request->getData());
if(!$animal->getErrors){
$image = $this->request->getData('image_file');
if($image !=NULL){
$name = $image->getClientFilename();
}
if( !is_dir(WWW_ROOT.'img'.DS.'animal-img') ){
mkdir(WWW_ROOT.'img'.DS.'animal-img',0775);
if($name){
$targetPath = WWW_ROOT.'img'.DS.'animal-img'.DS.$name;
$image->moveTo($targetPath);
$animal->image = 'animal-img/'.$name;
}
}
if ($this->Animal->save($animal)) {
$this->Flash->success(__('El animal se ha añadido.'));
return $this->redirect(['action' => 'index']);
}
}
$this->Flash->error(__('El animal no se ha podido añadir, por favor intentalo de nuevo'));
}
$allUsers = $this->getTableLocator()->get('User');
$user = $allUsers->find('list', ['limit' => 200])->all();
$this->set(compact('animal','user'));
}
My table after:
Image after save
The error:
1) App\Test\TestCase\Controller\AnimalControllerTest::testAdd
Possibly related to PDOException: "SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value"
…
Failed asserting that 500 is between 200 and 204.
I don't know why this is happening or how to know the reason. In the app the controller works fine. Data in the app:
Data in app
Data in test:
Data in test
I hope someone can help me, I don't know what to try anymore or how to know where the problem is...
I tried to look at the data but it doesn't apear to have any errors so I don't know where the error can be.
It was that the sql file used in the bootstrap didn't have the autoincrement value.
why this is not working where all my queries are individually working.
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $profile->phone,
'address' => $profile->address,
'gender' => $profile->gender,
];
return $data;
this works manually like
$data = [
'name' => 'my_name',
'email' => 'my_email',
'phone' => 'my_phone',
'address' => 'my_address',
'gender' => 'my_gender',
];
return $data;
my whole function is given below:
public function read($id){
$user=DB::table('users AS t1')
->select('t1.name','t1.email')
->where('t1.id',$id)->get();
$profile=DB::table('profiles AS t1')
->select('t1.phone','t1.gender','t1.occupation','t1.address')
->where('t1.user_id',$id)->get();
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $profile->phone,
'address' => $profile->address,
'gender' => $profile->gender,
];
return $data;
When using get() it returns a collection not a single object so you can do
public function read($id){
$user=DB::table('users AS t1')
->select('t1.name','t1.email')
->where('t1.id',$id)->first();
$profile=DB::table('profiles AS t1')
->select('t1.phone','t1.gender','t1.occupation','t1.address')
->where('t1.user_id',$id)->first();
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $profile->phone,
'address' => $profile->address,
'gender' => $profile->gender,
];
return $data;
Or if you have relations defined on the models you can use the relation
class User extends Model
{
public function profile()
{
return $this->hasOne(Profile::class);
}
}
class Profile extends Model
{
public function user()
{
return $this->belongsTo(User::class);
}
}
public function read($id)
{
$user = User::with('profile')->findOrFail($id);
$data = [
'name' => $user->name,
'email' => $user->email,
'phone' => $user->profile->phone,
'address' => $user->profile->address,
'gender' => $user->profile->gender
];
return $data;
}
Hi there I want to change this query into cakePHP 3.0 format but the code is not working could you guys can help me to solve this problem?
Normal SQL:
SELECT COUNT(user_tag_review.id) AS user_numbers, SUM(user_rating.rating_value) AS rating_value FROM user_tag_review INNER JOIN user_rating ON user_tag_review.id = user_rating.review_id WHERE user_tag_review.recruiter_id = 2 AND user_rating.rating_id = 1
CakePHP code that I have used:
$review_table = TableRegistry::get('UserTagReview');
$query = $review_table->find();
$query->select(["user_numbers" => $query->func()->count("UserTagReview.id")])
->hydrate(false)
->join(["table" => "user_rating",
"alias" => "r",
"type" => "INNER",
"condition" => ['r.review_id = UserTagReview.id']
])
->select(["rating_value" => $query->func()->sum("r.rating_value")])
->where(["UserTagReview.recruiter_id" => $recruiter_id, "UserTagReview.rating_id = " . 1]);
$result = $query->all()->toArray();
I think simpler way to migrate that SQL to Cake is fields options in finder:
$table = TableRegistry::get('UserTagReview');
$result = $table
->find('all' , [
'fields' => [
'user_numbers' => 'COUNT(UserTagReview.id)',
'rating_value' => 'SUM(user_rating.rating_value)'
],
'join' => [
[
'table' => 'user_rating',
'alias' => 'user_rating',
'type' => 'INNER',
'conditions' => [
'UserTagReview.id = user_rating.review_id'
]
]
],
'conditions' => [
'UserTagReview.recruiter_id' => 2,
'user_rating.rating_id' => 1
]
])
->hydrate(FALSE)
->toArray();
Maybe this code is not 100% correct, but focus on fields array - if we use this as associative array, we can evaluate any sql:
'fields' => [
'alias' => 'COUNT(IF(1,1,0))'
]
I have followed the following guide https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md but no file is being uploaded. below is my configuration
_form
This is in a for loop so i can get an array of different records.
<?= $form->field(new UploadForm , "[$count]file")->fileInput()->label(false) ?>
view
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>...
controller
if(isset(Yii::$app->request->post()['Factsheets'])){
for($i=0 ; $i < count(Yii::$app->request->post()['Factsheets']); $i++) {
//Yii::error(print_r(Yii::$app->request->post()['Factsheets'][$i],true));
if(!empty(Yii::$app->request->post()['UploadForm'][$i]['file'])){
$file = new UploadForm();
$file->file = UploadedFile::getInstance(Yii::$app->request->post()['UploadForm'][$i], 'file');
if ($file->file && $file->validate()) {
$file->file->saveAs('uploads/' . $file->file->baseName . '.' . $file->file->extension);
}
}
}
}
post log
[Factsheets] => Array
(
[0] => Array
(
[type] => image
[factsheet_id] => 1185
[path] => ../public/filespool/2/289/Pelotas_Reprocessing.jpg
)
[1] => Array
(
[type] => tech_paper
[factsheet_id] => 1433
[path] => ?basin=pelotas
)
[2] => Array
(
[type] => factsheet
[factsheet_id] => 1844
[path] => ../public/filespool/2/289/Pelotas_Reprocessing.pdf
)
)
[UploadForm] => Array
(
[0] => Array
(
[file] =>
)
[1] => Array
(
[file] =>
)
[2] => Array
(
[file] =>
)
)
I have noticed the following in the post log now. how do i construct it?
$_FILES = [
'UploadForm' => [
'name' => [
0 => [
'file' => 'Destin_Dome.jpg'
]
1 => [
'file' => ''
]
2 => [
'file' => 'Pelotas_Reprocessing.pdf'
]
]
'type' => [
0 => [
'file' => 'image/jpeg'
]
1 => [
'file' => ''
]
2 => [
'file' => ''
]
]
'tmp_name' => [
0 => [
'file' => '/tmp/phpoPgbJ9'
]
1 => [
'file' => ''
]
2 => [
'file' => ''
]
]
'error' => [
0 => [
'file' => 0
]
1 => [
'file' => 4
]
2 => [
'file' => 1
]
]
'size' => [
0 => [
'file' => 1129373
]
1 => [
'file' => 0
]
2 => [
'file' => 0
]
]
]
]
It seems like it is failing validation however i was able to make a copy like:
if(!empty($_FILES['UploadForm']['tmp_name']['file'])){
copy($_FILES['UploadForm']['tmp_name']['file'],"/tmp/".$_FILES['UploadForm']['name']['file']);
}
fileUpload model
public $file;
public $image;
public $factsheet;
/**
* #return array the validation rules.
*/
public function rules()
{
return [
[['file'], 'file','maxFiles' => 10],
[['image'], 'file','extensions' => 'gif, jpg'],
[['factsheet'], 'file','checkExtensionByMimeType' => false,'extensions' => 'pdf'],
];
}
I may be wrong but I think the problem is in your controller: your model rules are for multiple file upload and your form field too, but in the controller you set $model->file as a single UploadedFile instance. That is why your validation does not work. I can't give your advice (to change controller or model rule) because I do not clearly understand what are you doing here. It looks like you are creating multiple models from a single file upload field...
Im working on Zend Framework 2 especially with Zend Forms. I have declared a Select dropdown box in
Form:
$selectElement = new Element\Select('selectElement');
$selectElement->setAttribute('title', 'Select a Value')
->setAttribute('id', 'id');
$data = array(
array(
//Fetching the values from database
),
);
$selectElement->setAttribute('multiple', 'multiple')
->setValueOptions($data);
$this->add($selectElement);
InputFilter:
$inputFilter->add($factory->createInput(array(
'name' => 'selectElement',
'required' => false,
'filters' => array(
array(
'name' => 'Int'
),
),
)));
I have used Zend Debug to get the values which are in the selectElement dropbox in this fashion:
$dataSelectElements = $this->getRequest()->getPost('selectElement');
\Zend\Debug\Debug::dump($dataSelectElements);
Debug Result:
array(4) {
[0] => string(2) "20"
[1] => string(2) "22"
[2] => string(2) "23"
[3] => string(2) "75"
}
Basically Im getting the id's from the selectElement form to store it in the database. Right now Im getting a notice and zend form error:
Notice Error:
Notice: Array to string conversion in ..\zendframework\zendframework\library\Zend\Filter\Int.php on line 29
And a form invalid error:
array(1) {
[0] => array(1) {
["selectElement "] => array(1) {
["explodeInvalid"] => string(35) "Invalid type given. String expected"
}
}
}
Is there a solution to over come this problem. Any help would be appreciated.
The Int filter will attempt to make an Integer out of your array of data, which is not going to work.
Previously I've used the Callback filter, which can be used to loop through the data and check if each value is an Int.
For example:
'filters' => array(
array(
'name' => 'Callback',
'options' => array(
'callback' => function($values) {
return array_filter($values, function($value) {
return ((int)$value == $value);
});
}
)
),
),
I did bit differently, something like this
form
class Companyform extends Form
{
public function __construct()
{
// we want to ignore the name passed
parent::__construct('company');
$this->setAttribute ('method', 'post');
$this->setAttribute ('class', 'form-horizontal');
$this->add ( array (
'name' => 'parentID',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'parentID',
'type' => 'select',
'placeholder' => "Parent Company",
),
'options' => array(
'label' => 'Parent Company'
)
));
$this->add(array(
'name' => 'btnsubmit',
'attributes' => array(
'id' => 'btnsubmit',
'type' => 'submit',
'value' => 'Add',
'class' => 'btn btn-primary'
),
));
}
}
controller
public function addAction()
{
$request = $this->getRequest();
$companyList = $this->_getCompanyList();
$form = new Companyform();
$form->get('parentID')->setAttribute('options',$companyList);
if ($request->isPost())
{
$company = new Company();
$form->setInputFilter($company->getInputFilter());
$form->setData($request->getPost());
if ($form->isvalid())
{
}
}
}
public function _getCompanyList()
{
$companies = $this->Em()->getEntityManager()->getRepository('XXXX\Entity\Company')->findBy(array('isDeleted'=>'0'));
$companyIDList = array();
$companyIDList[0] = "No Parent";
foreach ($companies as $company)
{
$companyIDList[$company->id] = $company->companyName;
}
return $companyIDList;
}
Entity class
protected $inputFilter;
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'companyName',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 255,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
You may need to add following library in entity
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
In ZendFramework 2, when you creating a (add) element from your Form file, Check the attribute: inarrayvalidator is true.
$this->add(array(
'name' => 'select_name',
'type' => 'select',
'id' => 'select_name',
'options' => array(
'label' => 'Select Name',
),
'attributes' => array(
'id' => 'select_id',
'inarrayvalidator' => true,
),
));
I hope, this works...