This was my first crack at making a PUT request, after making a successful GET I was excited then following a tutorial I get an error that I don't quite understand.
Here is the error
"{"error":{"type":"ErrorException","message":"Undefined property: Symfony\\Component\\HttpFoundation\\ParameterBag::$name","file":"C:\\wamp\\www\\basketball-app-framework\\app\\controllers\\PlayersController.php","line":67}}"
I get that its on line 67 and the property is obviously undefined, but I followed the tutorial and I am not sure how else to write this, or why it's not working like the tutorial. I feel it could be because of mod_rewrite, I had an issue with that, and had to set some things in the apache config file just to make the GET request, so possibly I need to change some things to make the PUT.
Here is the code to PUT data.
public function update($id)
{
$input = Input::json();
$player = Player::find($id);
$player->name = $input->name; // this is line 67
$player->save();
}
again the error occours with $player->name = $input->name;
In my show method when I send a GET
public function show($id)
{
return Player::find($id);
}
Player::find($id); obviously works
so the $player variable inside my update shouldnt be causing an issue in accessing the name attribute, so I am at a loss and hoping someone can help this noob out.
Thanks.
Try the following:
public function update($id)
{
$input = Input::all();
$player = Player::find($id);
$player->name = $input['name'];
$player->save();
}
Or Try this:
public function update($id)
{
$player = Player::find($id);
$player->name = Input::get('name');
$player->save();
}
Related
Hello I have a problem with storing data to session using Symfony.
Here is my code:
public function currentAction() {
$session = $this->get('session');
$userArray = $session->get('test');
if($session->get('test') == null) {
$userArray = User::toArray($this->getUser());
$session->set('test', $userArray);
}
$this->setModel($userArray);
$this->sendResponse();
}
When I try to execute the code for the secound time the $session->get('test') still returns null. Can anybody helps me?
I also use (use Symfony\Component\HttpFoundation\Session;) from the beginning of my controller class.
I'm writing a code in PHP OOP and I'm trying to send $_POST data
filtered by one Class function to another Class function that will add
the data to database. Specifically login and password in registration
form.
I have 3 Classes that will do that:
Is simple Class that handles connection to database (I think it is not necessary to put code here)
Is the Class that filters the coming $_POST-s:
class Filter extends Dbconnect {
protected $login;
protected $haslo;
public function regFilter() {
if (isset($_POST))
{
foreach($_POST as $key => $val)
{
$filterVal = strip_tags($val);
$filterVal = htmlspecialchars($filterVal);
$filterVal = stripslashes($filterVal);
$filterVal = str_replace("\\", "", $filterVal);
$filter = array(
$key => $filterVal
);
foreach($filter as $key => $val)
{
echo "[$$key]";
echo "$val";
$
{
$key
} = $val;
}
}
return $filter = array(
'login' => $login,
'haslo' => $haslo
);
}
else
{
echo "Proszę podać login i hasło!";
}
}
}
Class that will get login and password and send it to DB:
class Dbinsert extends regFilter{
//protected $login;
//protected $haslo;
protected $query;
protected $dbo;
public function insert(){
//$this->extract = extract($filter);
//$this->login = $login;
//$this->haslo = $haslo;
$this->connect();
$this->query = "INSERT INTO uzytkownik (id, nazwa, haslo) VALUES ('', :login, OLD_PASSWORD(:haslo))";
if(!$result = $this->connect()->prepare($this->query)){
echo 'Zapytanie nie powiodło się';
}
else{
$result->bindParam(':login', $login);
$result->bindParam(':haslo', $haslo);
$login = $_POST['login'];
$haslo = $_POST['haslo'];
$result->execute();
}
$dbo = null;
}
}
Now when I try to send data from form with objects:
$rejestruj = new Dbinsert();
$filtruj = $rejestruj->regFilter();
var_dump($filtruj);
$dodaj = $filtruj->insert();
I get the following result:
[$login]login
[$haslo]password123
array(2) { ["login"]= string(5) "login" ["haslo"]= string(11) "password123" }
Fatal error: Call to a member function insert() on array in `E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\register.php` on line 78
Which doesn't surprises me since: login and haslo is returned from
"foreach" loop in class Filter (which is just for testing) "array(2)"
is returned from "var_dump($filtruj);"(to check if it is actually
working) and error is returned since I send an array to Class
Dbinsert - but in the function I put "extract" to get the variables.
How can I send just the variables from this filtered array to class
Dbinsert?
Edit: As #Twinfriends suggested I corrected function insert in class Dbinsert to actually use prepared statement, thats why (for now) login and haslo variables are reffering to $_POST. Now I need answer to my question.
(First time posting, thanks for edit suggestions, also any advice is appreciated since I'm quite the beginner
in PHP)
Sorry that it took so long to answer, I totally forgot your question. Well, lets take a look at your problem, hope to solve it.
I try to explain it as good as I can, so that you understand whats going on. First of all, lets look at your error message
Fatal error: Call to a member function insert() on array in
E:\Xampp\htdocs\php\bazy_danych\obiektowe\my\register.php on line 78
Okay. Call to a function on array... lets have a look at how you actually call the function:
$rejestruj = new Dbinsert();
$filtruj = $rejestruj->regFilter();
var_dump($filtruj);
$dodaj = $filtruj->insert();
And exactly here is your error. You have to understand that you call methods on objects and pass your data to this methods, not to call the methods on your data. What do I mean with that?
$rejestruj is your Dbinsert object. You create it in your first line of code here. Then, you call the regFilter function on it. Still anything is fine. As you see, var_dump gives you the expected results. So the error has to be on your last lane of code. And indeed, you try to call the method insert() on your array. And that won't work, since your array don't know any method called insert().
The right call to the method would be (Not the final one!!!):
$dodaj = $rejestruj->insert();
Now the method call should work. But in fact, it won't insert anything. Why? Because your insert() method try to bind the variables $login and $haslo - two variables the method don't know. So we need to pass the data in your method. To do that, you have to do the following changes:
Method call:
$rejestruj->insert($filtruj); // $filtruj contains your array
And your Dbinsert should look like:
class Dbinsert extends Dbconnect{
protected $query;
protected $dbo;
public function insert($data){
$this->connect();
$this->query = "INSERT INTO uzytkownik (id, nazwa, haslo) VALUES ('', :login, OLD_PASSWORD(:haslo))";
if(!$result = $this->connect()->prepare($this->query)){
echo 'Zapytanie nie powiodło się';
}
else {
$result->bindParam(':login', $data["login"]);
$result->bindParam(':haslo', $data["haslo"]);
$result->execute();
}
$dbo = null;
}
}
I hope your code works with this changes. So, while in my opinion the code should work now, I want to mention that there are many things you could improve. For example, you're not programming real "object-oriented" ... its more some pseudo OOP you're writing here. Some things are quite bad practice (could be done much easier). I don't want to dive to deep into details, since I don't know if you're interested in it. If you wish I can give you some more advises, but only if you wish.
Otherwise I really hope my answer help you. If the whole thing still doesn't work, please let me know so I can look at it again.
Have a nice day ;)
Edit:
Since it seems I haven't been clear enough, here the code how it should look like now:
$rejestruj = new Dbinsert();
$filtruj = $rejestruj->regFilter();
$dodaj = $rejestruj->insert($filtruj);
Before explaining the problem. Let me show the controller function:
public function storePost(IdeaRequest $request)
{
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = $request->input('user_id');
$idea->idea_image = $request->file('idea_image')->move('publicPages\images')->getClientOriginalName();
$idea->idea_info = $request->input('idea_info');
$idea->selection = $request->input('selection');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = $request->input('idea_description');
$idea->save();
session()->flash('flash_message', 'Your idea has been submitted for Review');
return back();
}
It stores the image as .tmp file. Things I have tried out
guessExtension(), It just returns the extension name and does not
even store the image.
getClientOriginalName(), it throws an error;
getClientOriginalName method is not defined. I have searched the
method and it is in there. used its namespace
Symfony\Component\HttpFoundation\File\UploadedFile. it did not work
either.
Tried different things out from stackoverflow, Nothing has worked
for me till now. In other words, i have invested a lot of time to
solve this problem but nothing worked. Any help would highly be
appreciated.
Here you go:
public function storePost(IdeaRequest $request)
{
$request->file('idea_image')->move('publicPages\images');
$filename = $request->file('idea_image')->getClientOriginalName();
$idea = new Idea();
$idea->idea_title = $request->input('idea_title');
$idea->user_id = $request->input('user_id');
$idea->idea_image = $filename;
$idea->idea_info = $request->input('idea_info');
$idea->selection = $request->input('selection');
$idea->idea_location = $request->input('idea_location');
$idea->idea_goal = $request->input('idea_goal');
$idea->idea_description = $request->input('idea_description');
$idea->save();
session()->flash('flash_message', 'Your idea has been submitted for Review');
return back();
}
Observe closely. You will have to do this in 2 separate lines:
$request->file('idea_image')->move('publicPages\images');
$filename = $request->file('idea_image')->getClientOriginalName();
Why?
move() returns a File object representing the new file.
The File class doesn't have a getClientOriginalName() method. That method belongs to UploadedFile.
When you chain them, you are trying to access getClientOriginalName() from File, which doesn't exist. Have a look at the docs here.
However...
File extends the PHP native SplFileInfo class which has a getFilename() method. So I guess you could also do:
$request->file('idea_image')->move('publicPages\images')->getFilename();
I get a strange PHP error after updating my php version to 5.4
This is my function
protected function create() {
//if (VBRIDGE_DEBUG)
//drupal_set_message(__CLASS__ .'::'.__METHOD__);
$path = $this->vbridge_root_path;
$path_vbridge = $path . '/' . VBridge::VBRIDGE_CLASS_PREFIX;
$subclass = $this->getClass();
foreach ($this->_objclass as $objclass) {
if (!$this->createObj($path, $objclass, $subclass)) {
$this->createObj($path_vbridge, $objclass);
}
}
if (self::getStatus()) {
return false;
}
// Set User Session Qookie
//$this->getUser()->setQookie($this->getQookie());
// Set User Session
$this->getUser()->setSession($this->getSession());
$this->getSession()->setQookie($this->getQookie());
//$this->getUser()->setAuth($this->getAuth());
// Set User Pass
$this->getUser()->setPass($this->getPass());
// Set Auth
$this->setAuthMethods();
$this->setAuthStorages();
//
foreach ($this->getConfig() as $config) {
if ($config['#type'] == '#class') {
//createObj($config['#name'], $config['#type'], $config['#class'], $config['#path'], $appData['#config']);
}
}
return true;
}
This is the line that gives
if ($config['#type'] == '#class') {
I've looked at similar questions but haven't figured out how to fix this. Any assistance would be helpful.
Edit: Yes, I did put wrong code up last night. I was very tired after trying to tangle with this.
The error message you've posted doesn't match the line of code you say it originates from. You should get a different error for that, specifically to do with only using variables by reference.
Your code should be
$accounts = user_load_multiple(array(), array('name' => $login));
$account = array_shift($account);
But Drupal already has a helper method for that, so you might as well use it:
$account = user_load_by_name($login);
I'm currently developing a payment method and things are working quite well.
Just one thing: The customer enters some information along the payment method and through debugging I can see that it gets written into the InfoInstance via Mage_Payment_Model_Method_Abstract::assignData()
Unfortunately, I can't read that data when I'm in the capture()-Method. I retrieve the InfoInstance and try to read the information, but it's not set.
assignData() method:
public function assignData($data) {
if (!($data instanceof Varien_Object)) {
$data = new Varien_Object($data);
}
$info = $this->getInfoInstance();
$info->setEtixType($data->getEtixType());
return $this;
}
capture() method:
public function capture(Varien_Object $payment, $amount) {
// ...
$info = $this->getInfoInstance();
Mage::log('etix_type: '.$info->getEtixType()); //I expect something like "etix_type: cc"
// ...
}
Any help is appreciated. I'm sure I missed something.
Found it,
Assigning veriables directly to the InfoInstance works, but it does not persist through the whole checkout process. Instead, you have to set it on the additional_data:
$info = $this->getInfoInstance();
$info->setAdditionalInformation('etix_type', $data->getEtixType());
And later you can read it via:
$info = $this->getInfoInstance();
$etix_type = $info->getAdditionalInformation('etix_type');