Array to string conversion error in php laravel - php

I am creating an event based website. I am using laravel for this with backbone. I am retrieving data from database using backbonejs with laravel as following
var events = new Events();
new EventsView({el: $("#calendar"), collection: events}).render();
events.fetch();
On the server side I have following controller
class Calendars_Controller extends Base_Controller {
public $restful = true;
public function get_index()
{
//print_r(Calendar::all() );
return Calendar::all();
}
}
But this returns following error
Unhandled Exception
Message:
Array to string conversion
Location:
/Applications/MAMP/htdocs/calendar/laravel/response.php on line 272
How can I solve this issue?

If you're not using Laravel 4, you might want to try returning the following from your controller instead:
$events = Calendar::all();
return Response::eloquent($events);

Related

Attempted to call an undefined method named "getChoices" of class "Symfony\Component\Form\ChoiceList\View\ChoiceListView"

In the app updated from Symfony 2.3 to 2.8, we have changed the choice list that changed in 2.7.
See the links below for documentation on previous code and changes.
I got the following error in it.
Does this mean I have to create getChoices in StaffChoiceLoader?
I think only loadChoiceList, loadChoicesForValues, loadValuesForChoices required for ChoieLoaderInterface is needed.
Beforecode
Symfony: Unable to reverse value for property path [...]: The choice [...] does not exist or is not unique
Document
https://github.com/symfony/symfony/blob/2.8/UPGRADE-2.7.md
ChoiceLoader.php
class StaffChoiceLoader implements ChoiceLoaderInterface
{
public function loadChoiceList($value = null)
{
$staffs = $this->staffService->getStaffByShop($this->loginStaff->getShop());
$factory = new DefaultChoiceListFactory();
$choiceList = $factory->createListFromChoices($staffs);
$choiceListView = $factory->createView(
$choiceList,
function ($staffs, $key) use ($staffs) {
return $staffs[$key];
});
return $choiceListView;
}
}

How to 3rd party application access to Yii2 (HumHub)

I want to give a 3rd party PHP application access to Yii2 user data (HumHub) and have tried this:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig));
$user = Yii::$app->user->identity;
return $user;
}
This does not work. There are no errors up until new humhub\components\Application($yiiConfig) but then the 3rd party app breaks with no error thrown and the function does not return anything.
I did find this solution which does not work.
Is there are reason this does not work or is there an alternate solution to getting Yii2 user data properly?
This is how to do it in HumHub V1.0
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$config = yii\helpers\ArrayHelper::merge(
require('../protected/humhub/config/common.php'),
require('../protected/humhub/config/web.php'),
(is_readable('../protected/config/dynamic.php')) ? require('../protected/config/dynamic.php') : [],
require('../protected/config/common.php'),
require('../protected/config/web.php')
);
new yii\web\Application($config); // No 'run()' invocation!
Now I can get $user object:
$user = Yii::$app->user->identity;
Indeed an error should be thrown, but, the error settings of your PHP may be overridden or set to not display errors.
You call undefined object Yii::$app->user->identity. The reason, from documentation because you have not initialized the Yii object. So your code should be as follows:
function getUserId() {
require_once('../protected/vendor/yiisoft/yii2/Yii.php');
$yiiConfig = require('../protected/config/common.php');
(new humhub\components\Application($yiiConfig)); // try to comment this line too if it does not work
// Add The Following Line
new yii\web\Application($yiiConfig); // Do NOT call run() here
$user = Yii::$app->user->identity;
return $user;
}

Yii2 rest api update throws object conversion error

I am working with Yii2 REST api and using Authorisation : Bearer for authentication.
I have a model Event and only 2 actions Create and Update but my Updateaction is not working fine and throws Object Class conversion error.
I am using following code for finding Event model with mixed condition.
public function actionUpdate($id)
{
$params=$_REQUEST;
/*Following line throws error */
$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id'=> Yii::$app->user->identity]);
if($model !== null){
$model->attributes=$params;
$model->partner_id = Yii::$app->user->id;
$model->updated_date = time();
if ($model->save()) {
$this->setHeader(200);
echo json_encode(array('status'=>1,'data'=>array_filter($model->attributes)),JSON_PRETTY_PRINT);
}
}
}
The error is something like this
Object of class api\modules\v1\models\User could not be converted to string
I cant figure out why it says i have created object of User class.
Yii::$app->user->identity
is object you should use
Yii::$app->user->identity->id
so final line will be:
$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id'=> Yii::$app->user->identity->id]);
The problem is with your andWhere(), you are trying to assign partner_id an object viz. Yii::$app->user->identity, so this is where your code is breaking. And do not use json_encode when you can use Yii's response format Response::FORMAT_JSON, so your code would be like:
public function actionUpdate($id)
{
\Yii::$app->response->format = yii\web\Response::FORMAT_JSON; // formatting response in json format
$params= json_decode(\Yii::$app->request->rawBody, 1);
/*Following line throws error */
$model = Event::find()->where(['event_id'=>$id])->andWhere(['partner_id'=> Yii::$app->user->identity->id]);
if($model !== null){
$model->attributes=$params;
$model->partner_id = Yii::$app->user->id;
$model->updated_date = time();
if ($model->save()) {
$this->setHeader(200);
return array('status'=>1,'data'=> $model); // you can simply use $model
}
}
}
The issue is here:
andWhere(['partner_id'=> Yii::$app->user->identity])
You ARE attempting to convert a user object (Yii::$app->user->identity) to a string. Instead, you need to use the user's id (Yii::$app->user->identity->id) which is a string.

Fatal error: Class 'Zend_Log' not found

I'm getting the following error in magento administration
Fatal error: Class 'Zend_Log' not found in /home/website/public_html/app/code/community/Uni/Fileuploader/Block/Adminhtml/Fileuploader/Edit/Tab/Products.php on line 241
This is a community extension, which has been working fine on my website. The error makes no sense to me, because the line 241 contains just a closing "}" character.
class Uni_Fileuploader_Block_Adminhtml_Fileuploader_Edit_Tab_Products extends Mage_Adminhtml_Block_Widget_Grid {
...
...
...
public function getRowUrl() {
return '#';
}
public function getGridUrl() {
return $this->getUrl('*/*/productgrid', array('_current' => true));
}
protected function getFileuploaderData() {
return Mage::registry('fileuploader_data');
}
protected function _getSelectedProducts() {
$products = $this->getRequest()->getPost('selected_products');
if (is_null($products)) {
$products = explode(',', $this->getFileuploaderData()->getProductIds());
return (sizeof($products) > 0 ? $products : 0);
}
return $products;
}
} // line 241, where error occurs
I can post the rest of the code, if you need it.
I noticed that if I upgrade to PHP 5.4 version the error disappears, but since 5.4 version causes other errors on my website, I have to continue using 5.3.
Any ideas on how to solve this?
The problem could be the name of one of the methods in your custom class.
Take for example the method name is getData() ,
Try searching for generic method names in your script, such as getData, which might be reserved by some of Magento’s core classes. I figure that these methods have predefined functionality, which your module is missing support for, and Zend then tries to write an exception to Zend log.
Reference link: netismine
I got the same error when rewriting a payment method.
public function authorize($payment, $amount)
Solved rewriting exactly the same main method:
public function authorize(Varien_Object $payment, $amount)
Magento 1.9.1.0/PHP 5.5

Controller dispatch error in Zend framework 2

I am returning Json response from zend controller.
This is my code
Controller Code
public function get()
{
$result = //calling other function and getting response in array
print("value in controller before calling toJsonModel");
print_r($result);
$temp = $this->toJsonModel($result);
var_dump($temp);
return $temp;
}
toJsonModel function
public function toJsonModel($data = array())
{
$data['requestId'] = $this->getHeader('RequestId');
print("Value just before returning to controller");
print_r($data);
return new JsonModel($data);
}
Response
First and second print displays correct values. But after getting values from toJsonModel when I try to displays wrong values and adds some other values like "captureTo", "terminate" and "children" as protected.
I don't know why it's giving me wrong message.
Can any one guide me to right path and tell me what the problem is ?
Thanks in advance.
EDIT
I am adding new screenshot to display full message.
ERROR :
{"name":"Email Not Found","message":"No account found with this email
address.","code":404,"requestId":"12","reason":"error-controller-cannot-dispatch","display_exceptions":true,"controller":"RSMobile\Controller\ForgotPassword","controller_class":null}
I finally solved this problem.
I was throwing some error from controller whenever there is a problem. I used to throw 404 when email not found. That was causing this error.
At first i thought that this is a problem related to JSON model. But thanks to #Bilal who helped me in figuring out the problem.
When I throw 404 from controller it by default appends all these data at the end. But when i throw any error other than 404, it works.
This is because you are returning an object of JsonModel from method toJsonModel.
return new JsonModel($data);
So, every object has some properties that you can use to manipulate data. See the docs of Zend\View\Model\JsonModel here
So actually the values are not wrong but there is a way to access properties of an object.
Update
Attaching dispatch event
public function onBootstrap($e)
{
// some stuff
$eventManager = $e->getApplication()->getEventManager();
$eventManager->attach("dispatch", function($e) {
echo "Dispatch!";
});
// some stuff
}

Categories