I'm trying to perform a mass assignment of 2 variables I'm sending via GET to another model::controller (from project::actionCreate to client::actionCreate)
In the _form view for project::actionCreate I've got the following:
<?php echo " ".Chtml::link('+New client',array('client/create',array('Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id))));?>
With the goal of creating an array "Client" with attributes "redir" and "redirId".
In client::actionCreate I want to do something like
if(isset($_GET['Client']))
{
$model->attributes=$_GET['Client'];
}
Now I noticed that my $_GET var puts client inside subarray 0, so I've tried this with
$_GET[0]['Client']
as well, but no luck. However if I manually assign the variables like this:
$model->redir = $_GET[0]['Client']['redir'];
$model->redirId = $_GET[0]['Client']['redirId'];
Then it works.
Any idea what is up? The goal is to allow someone to create a new client while creating/updating a project record, by sending them to client::actionCreate, but redirecting them back to their original project::actionCreate if they were linked there from my "+New Client" link.
I think the client array is put inside subarray 0 because you've added an array around the parameters. Try removing the array like the following:
<?php
Chtml::link('+New client',array('client/create', 'Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id)));
?>
I don't know what your model looks like but if the fields aren't assigned they are probably not safe. You can make them safe by adding them to the rules part of your model. Or you could try the following, by specifying the false parameter it will be possible to assign values to unsafe attributes. (http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail)
$model->setAttributes($_GET['Client'], false);
I am not sure creating a link like you want is possible. I have asked something similar some time ago Yii link with [ as a parameter I just could never get the link to how I wanted it. In the end I just created the link the old fashion way, not using CHTML.
Related
Hopefully this makes sense to someone.
I'm using Kartik-V Select2 widgets in a ActiveForm, with 'multiple' and 'tags' enabled. I'm using this elsewhere in my application and it is working fine there, and on submission the array data is sent as I would expect in the appropriate form field. From there I can easily implode the array and save to database in the controller action.
However in a different section of my app, with exactly the same configuration in the view, model and controller, the array is being posted as an additional form field (as can be seen watching the post event in the firefox console).
What I'm seeing in the firefox console is:
-----------------------------366976194315951562394252057249
Content-Disposition: form-data; name="Notes[notes_to]"
-----------------------------366976194315951562394252057249
Content-Disposition: form-data; name="Notes[notes_to][]"
admin
-----------------------------366976194315951562394252057249
Content-Disposition: form-data; name="Notes[notes_to][]"
kennettm
So where I would expect to receive the array in Notes[notes_to], it is actually coming through in Notes[notes_to][] but I can't for the life of me figure out how to access and handle these values in my controller, or how to get the array of tags submitted in Notes[notes_to] where I want it.
Thanks in advance.
***** EDIT *******
Thanks #Muhammed. I am expecting to received an array ['admin','kennettm'] (as with other areas of my app where I've used this setup), however in this instance that is not what is happening.
I've got the model and CRUD generated by Gii for the Notes section, so it does have its own MVC setup however the notes _form file in this case is rendered on the site index, so in this case the model is being loaded in the Site controller and is handled there. I can confirm that the same issue is occurring regardless of where I render the _form file, be it in the index or in the Notes section view.
I would expect that the data be returned in $notesmodel->notes_to as an array as mentioned above, which is then easily imploded and saved to db. However the data is not there on form submission, and "Public" is being saved every time. For the sake of testing, I removed the if statement, set the controller to implode the array and ensure that I add tags (so the array is sure to be there), however then I am receiving an error exception "implode(): Argument must be an array".
Snippet of my intended controller code is below. The controller checks to see if any tags have been added to the field. If tags are present, there should be an array which can be imploded and saved. Otherwise if no tags are present, it will revert back to a simple string "Public' and save that instead.
$notesmodel = new Notes();
if ($notesmodel->load(Yii::$app->request->post())) {
$notesmodel->notes_user = Yii::$app->user->identity->username;
$notesmodel->notes_created = date("Y-m-d H:i:s");
if (!empty($notesmodel->notes_to)) {
$notesmodel->notes_to = implode($notesmodel->notes_to);
} else {
$notesmodel->notes_to = 'Public';
}
I am having difficulty with the proper layout for this piece of code I have been talking with another IT support person and we are both at a bit of a loss
I am trying to utilize an API call and I am trying to come up with the proper syntax
I am using a PHP POST call and CURL along with an array of variable names and values for the most part it works
The problem is that a few variable names are not recognized and the other support person thought we needed to change things up a bit
The documentation states there may be a change we need to setup these variable ahead of time
the structure needs to be something like this CDATA block:
<profileFieldValues>
<fieldValue id="_sys_firstname">
<value>Jeff</value>
</fieldValue>
<fieldValue id="_sys_lastname">
<value>Johnson</value>
</fieldValue>
</profileFieldValues>
WE are using a structure like this
$fields = array(
'restype' => '2',
'username' => 'abc123',
...,
)
So what would the profileFieldValues block of code be written to work in the $fields array?
We were trying something like this but it was not correct
'profileFieldValues'=>"fieldValue id[]=>('_sys_firstname=>'value'=>'Charlie','_sys_lastname=>'value'=>'Brown')",
we received an error with this:
The user profile field elements are not properly formatted
hi i create one custom page in WordPress like page-download.php
i access this page like
http://example.com/download
its word fine but i want access this page like
http://example.com/download?n=skype
http://example.com/download?n=firefox
etc every time n value change.
and also i want get n value in page-download.php
please tell me how to do it . i know how to work in php simple but in wordpress its not work
Why just not use, echo $_GET['n'] ?
When you hit this page directly from the URL like below, please change your parameter, because of that I have tried same method few days ago and I can't get the parameter value.
http://example.com/download?from_browser=skype
http://example.com/download?from_browser=firefox
You can get the value like below or as per the reference :
$blah = get_query_var( 'from_browser');
if(isset($blah)) // Your code will be here
Hope this help!!!
I have some questions concering routing with Codeigniter. What I´m doing now is the following:
$route['articles/(:num)'] = 'articles/view/$1'; // $1 will contain an ID
This means that example.com/articles/123 will work perfectly and load an article with an ID of 123. But I also want to have the possiblilty to add the aticle´s title to the URL (for SEO). Example: example.com/articles/123/article-title
What I want is pretty much the same thing as Stack Overflow: stackoverflow.com/questions/123/the-title
How can I do that?
I´m also wondering how Stack Overflow works. If I go to stackoverflow/questions/111 the title will automatically be added to the url. Is that done with php redirect()?
I have done something similar in the past; I can't find it know but IIRC (it was months ago) You can use a route like you did, and also add a more specific one, like
$route['articles/(:num)/(:any)'] = 'articles/view/$1/$2';
$route['articles/(:num)'] = 'articles/view/$1';
As you can see, both map to the same method, which is kind of "overloaded"; you can make up for a missing parameter by using a default value:
function articles($id,$slug = FALSE)
{ }
and simply ignore the second parameter in your article retrieval.
As for adding the title you can:
have a "slug" field in your database, created when the article is saved. You can use the comfortable url_title($title,'dash',TRUE) function (in the url helper), which takes the $title, uses the dash as separator, and make it all lowercase;
use the above function and convert the title of the article (after you retrieved it from the database) "on-the-fly"; just check on the article() method if the 2nd parameter isn't false and you'll know if you need to create the slug or not;
As for how to show the slug even when using an url without it you can make, as you guessed, a redirect, but since both routes point to the same method it won't change anything for you.
Oh, uhm, beware of loops while calling the redirect, check carefully ;)
I suggest you to create a slug field into your table and complete it with the url you want to use as id.
Let me explain.
You have this table
id
title
slug
when you save an article into your db you can dinamically create a slug, for example:
id: 1
title: My first post
slug: 1-my-first-post
then you can use the slug (in this case 1-my-first-post) ad id for the page, you can call it:
www.example.com/articles/1-my-first-post
obviusly you need to handle it in your db slect
As we discussed on the comments.
You can create a route several times and with different parameters each, like:
$route['articles/(:num)/(:any)']
$route['articles/(:num)']
I would create a function with a redirect, adding or not the title to it.
Hope it helps.
I was used to putting id's in the URL to map to an item in the database:
/hotels/1
but what if I want to get the name of the hotel with an id of 1 and put it in the URL and replace spaces with hyphens?
/hotels/hotel-bianca
I am using Kohana and there is the concept of routing (which is pretty much present in all MVC frameworks), but I can't seem to get it working
How do I go about doing this?
Since I know nothing about kohana, I am going to present a possible PHP answer.
Could you pass the id through the URL and request it with PHP, and if you're passing the name of the hotel, have that correspond to the item in the database with the hotel-name as its field?
For this purpose I use special field in db table named url :)
So for example /controller/open/urladress will look for url field with 'urladress' inside to open :D
I don't think you can change uri on fly)
In the controller you could search the database for the name. I am used to Kohana 2.3.4 with ORM so this is how I would do it:
// first you need to replace all hyphens with spaces
$name = str_replace('-', ' ', $parameter);
// search your db for the hotel by name
$hotel = ORM::factory('hotel')->where('name', $name)->find();
// check to make sure it is loaded
if ( ! $hotel->loaded)
{
// Do something i.e. 404 page or let them know it wasn't found
}
This would let you specify by name. You could also add a check to see if the parameter was integer or not and search by id if it was.