Creating URL and params in Yii2 - yii2

I'm trying to create Url with multiple param using Url::to() method but it's not working i also tried UrlManager->creatUrl() method but no luck, i have pretty Url enable and if i use only one query param it works fine but when i try to add more i got error, below is my code.
$linkUrl = \Yii::$app->UrlManager->createUrl(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
<?= Html::button('Change category or product',['value'=>$linkUrl, 'id' => 'StartOver']) ?>
another try is:
$linkUrl = Url::to(['sell/updatecategory', ['draftId'=> $model->draftId,'catid' =>$model->category_id]]);
in the two case above the url output always look like this:
GET http://http://192.168.199.128/trobay/products/sell/updatecategory?1%5BdraftId%5D=20&1%5Bcatid%5D=50
and the server throw an error cannot resolve the url, what i want is something like this:
GET http://http://192.168.199.128/trobay/products/sell/updatecategory?draftId=20&catid=50
The system added some character which i guess is the cause of the problem but don't really know how to remove this. i hot anyone could help with this thanks

Don't use nested array for parameters.
Structure should look like this:
[0 => route, param1 => value1, param2 => value2, ...]
so in your case
$linkUrl = Url::to([
'sell/updatecategory',
'draftId' => $model->draftId,
'catid' => $model->category_id
]);

Related

How can I have the name of my entity instead of the id in the related tables

I'm creating a project on CakePHP 3.x where I'm quite new. I'm having trouble with the hasMany related tables to get the name of my entities instead of their ids.
I'm coming from CakePHP 2.x where I used an App::import('controller', array('Users') but in the view to retrieve all data to display instead of the ids, which is said to be a bad practice. And I wouldn't like to have any code violation in my new code. Can anybody help me? here is the code :
public function view($id = null)
{
$this->loadModel('Users');
$relatedUser = $this->Users->find()
->select(['Users.id', 'Users.email'])
->where(['Users.id'=>$id]);
$program = $this->Programs->get($id, [
'contain' => ['Users', 'ProgramSteps', 'Workshops']
]);
$this->set(compact('program', 'users'));
$this->set('_serialize', ['ast', 'relatedUser']);
}
I expect to get the user's email in the relatedUsers of the program table but the actual output is:
Notice (8): Trying to get property 'user_email' of non-object [APP/Template\Asts\view.ctp, line 601].
Really need help
Thank you in advance.
You've asked it to serialize the relatedUser variable, but that's for JSON and XML views. You haven't actually set the relatedUser variable for the view:
$this->set(compact('program', 'users', 'relatedUser'));
Also, you're setting the $users variable here, but it's never been initialized.
In addition to #Greg's answers, the variable $relateduser is still a query object, meaning that trying to access the email property will fail. The query still needs to be executed first.
You can change the query to:
$relatedUser = $this->Users->find()
->select(['Users.id', 'Users.email'])
->where(['Users.id' => $id])
->first();
Now the query is executed and the only the first entry is returned.
There is are a number of ways to get a query to execute, a lot of them are implicit is use. See:
Cookbook > Retrieving Data & Results Sets

Laravel eloquent add to JSON

To begin with, sorry for my bad English. I have column:
$table->json('images')->nullable();
Table is called albums. In images I would like to store an array with image names, but how can I every time add to that json? My code right now:
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$album->update([
'images' => $request->image->name <= tried to do something.. My uploader everytime calls this function, so I need to get current IMAGES column value, and add new. Like ['first image name', 'second image name'], after update this must be ['first image name', 'second image name', 'third image name'] not just ['third image name']
]);
$album->save();
I need to do like laravel increment function, but which works only with int looks like. How can I do that? Thanks in advance!
I think its not possible to directly change the JSON as your images data would return as a String.
First get the JSON as a string, then update the Object and update the JSON.
So something like:
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$images = json_decode($album->images);
array_push($images, $request->image->name);
$album->update(['images' => $images]);
$album->save();
Note: I didn't check this code, but this should give you an idea of how to handle this
Good luck
$album = Album::where('hash', $request->album_hash)->firstOrFail();
$arra = json_decode($album->images);
$arra[] = $request->image->name;
$album->update([
'images' => $arra
]);
$album->save();
I hope help you

Get variable GET in Yii2 not working

Basiclly I have an url that have a parameter.
So, I need to access those parameter using GET. This is the GET that coming:
Array
(
[r] => hanwa/incoming-pipe/assign-incoming
[1] => Array
(
[min_urut] => 1
[max_urut] => 44
)
[_] => 1496645704980
)
In docs, docs,
We can use like this :
echo $request->get('min_urut');
But, I got nothing. Please Advise.
Use Concept of ArrayHelper class :-> http://www.yiiframework.com/doc-2.0/guide-helper-array.html#getting-values
ex:
$data = ArrayHelper::getValue($request->get(), 'Temp.yourvalue.yourindex');
Main use of $request->get() method mainly returns to you a value from $_GET,
so example is
$temp = $request->get('Temp'); // Here $temp variable contains $_GET['Temp']
$data = $temp['yourvaluename'][0];
Judging by the fact that you have an array with the key [r] you have an associative array, method get() you will not get it, you need to do it just like this ..
echo $request->get()[1]['min_urut'];

How to use Url::remember in yii2

I want to create a link on my error page to take user back to the previous link.
Suppose the current URL is http://example.com/site/product, and a user try to view
http://example.com/site/product?id=100 and a product with id =100 does not exit, the system should throw 404 error to the error page, now if i want to create a link to take the user back to http://example.com/site/product the previous URl how do I make this work. i can make this work by hardcoding this in my error views file, but i want it dynamically as i have many controller an action using the same view file.
I try this in my site conteoller
controller/site
public function actions()
{
$url = Url::remember();
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
$this->render('error',['url'=>$url]),
];
}
and try to get the value the in error view file like this
/views/site/error.php
<p>
<?= Html::a('go back', [$url)?>
</p>
but it has no vaule..
please any good idea on how to make this work, am also open to new solution
this is form Yii2 Guide http://www.yiiframework.com/doc-2.0/guide-helper-url.html#remember-urls
There are cases when you need to remember URL and afterwards use it
during processing of the one of sequential requests. It can be
achieved in the following way:
// Remember current URL Url::remember();
// Remember URL specified. See Url::to() for argument format.
Url::remember(['product/view', 'id' => 42]);
// Remember URL specified with a name given
Url::remember(['product/view', 'id' => 42], 'product');
In the next
request we can get URL remembered in the following way:
$url = Url::previous();
// or
$productUrl = Url::previous('product');

How to pass a new variable to template.xml file

I have installed my own custom theme to xenforo. In my template.xml file i can see the usage of some variable like {$requestPaths.fullBasePath} in the head section. If i want use another variable like these in the header section, where should i defined this variable and from where we can assign values o that variable?
You can do that in the controller, something similar to this:
$viewParams = array(
'variableName' => $variableValue,
'variableName2' => $someOtherValue,
'someArray' => array(
'foo' => 'bar'
)
);
return $this->responseView('MyAddOn_ViewPublic_SomeViewClass', 'some_template', $viewParams);
Then in your template, you can use those variables with the curly syntax:
{$variableName} // output $variableValue with html escaped
{xen:raw $variableName2} // output $someOtherValue
{$someArray.foo} // output "bar"
There are other ways to pass variables to template too: using template_create event listener or <xen:container /> but that's quite complicated. For more information regarding add-on development, read here.