save user to row from frosuserbundle - php

I got the following error:
Catchable Fatal Error: Argument 1 passed to Rowoco\AllgemeinBundle\Entity\Place::setIdUser() must
be an instance of Rowoco\AllgemeinBundle\Entity\FosUser, instance of
Rowoco\UserBundle\Entity\User given, called in
/var/www/symfony/src/Rowoco/AllgemeinBundle/Controller/AllgemeinAjaxController.php on line 64 and
defined in /var/www/symfony/src/Rowoco/AllgemeinBundle/Entity/Place.php line 331
In the following function i try to add a new place:
public function test()
{
$em = $this->getDoctrine()->getManager();
$stateRepo = $em->getRepository( "RowocoAllgemeinBundle:State" );
$stateEntity = $stateRepo->find(1);
$user = $this->get('security.context')->getToken()->getUser();
$place = new Place();
$place->setAdddate( "2014-01-01 12:12:10" );
$place->setTitle( "test" );
$place->setDescription( "testdescription" );
$place->setSpecial( "special" );
$place->setStreet( "strasse" );
$place->setZipcode( "12345" );
$place->setIdState( $stateEntity );
$place->setIdUser( $user );
$em->persist( $place );
$em->flush();
//return new Response('Created product id '.$product->getId());
return true;
}
in the table place i have two foreignkeys
id_state
id_user
how can i fix this error?

Somethings around your code:
First the error is telling you what the problem is and where, take a look at this line:
Catchable Fatal Error: Argument 1 passed to Rowoco\AllgemeinBundle\Entity\Place::setIdUser()
must be an instance of Rowoco\AllgemeinBundle\Entity\FosUser, instance of
Rowoco\UserBundle\Entity\User
You're passing a complete object to setIdUser() method which BTW expect a FOSUser instance
Second, if you're using Symfony > 2.1.x you should use $this->getUser() and not as you're doing (this is a tip)
As third if you only need to insert the id_user as state in question, why pass the entire object? Why not just pass the current logged in user id? Something like $this->getUser()->getId()?

Related

Trying to acess data of an object, says inaccessible

i am trying to access property of object but while it shows the data is object but when i try to access the property it gives error 'Use of undefined constant 'id';
the code is ;
function returnProducts(Request $request,$id){
$user = Auth::user();
if($user->hasRole(['customer','bos_customer','admin','vendor','business_owner',
'merchant','city_manager','city_order_manager'])){
$sale = Sale::find($id);
$saleDetail=$sale->saleDetails;
$num=count($saleDetail);
return $request->product_ids[id];
the input recieved from the front-end is called from
return $request;
how can i access the object property i tried accesing it but cannot wrap my head around the problem i am facing
problem probably is here:
change this:
return $request->product_ids[id];
to:
return $request->product_ids['id'];
or:
return $request->product_ids[$id];

Call static method in PHP across variable

I working in Laravel project and I can't call static method across variable
For example:
$objName = 'User';
$objName::get();
On this way I get error.
$objName = 'User';
Is a string, for using the get() method $objName should be an object, for example:
$objName = User::all()->first(); // this will return an object
Ok I use
User::all();
But I want get parametar from URL for example www.example.com/User, www.example.com/Articles -> User and Article is parametar in URL (this is Laravel web route) and call static method. When I wrtie first URL than call User object if I write first URL than call Article object.
www.example.com/User
$param= 'User';
$param::all();
www.example.com/Article
$param= 'Article';
$param::all()
I find solution!
This solution in Laravel:
$data = call_user_func( array('\App\\'.$param , 'all'));
but if you want use in plain PHP then is:
$data = call_user_func( array($param , 'all'));
This will call Object and Method.
If you want to send arg in methond thent is:
$data = call_user_func( array('\App\\'.$param , 'all'), $arg); /*For Laravel*/
$data = call_user_func( array($param , 'all'), $arg); /*For plain PHP*/

Fracture Transform throws boolean given

I am using a Transformer in my Laravel project. When I don't include an other object in the Transformer there isn't any problem but when I include the Customer object I get the following error:
Argument 1 passed to App\Transformers\CustomerTransformer::transform() must be an instance of App\Models\Customer, boolean given, called in /home/vagrant/Code/project/vendor/league/fractal/src/Scope.php on line 365 and defined
When I printed the object from Scope.php there weren't any booleans in it. What could be the problem? (The code crashes after Review #298.
How I call the code:
$reviews = $this->review->paginate();
$transformer = new ReviewTransformer();
$with = $request->get('with', null);
if($with) {
$with = explode(';', $with);
$transformer->parseIncludes($with);
}
return $this->response->paginator($reviews, $transformer);
Fixed the problem, I'm an idiot..
I had the following include in my Transformer class:
public function includeCustomer(Review $review)
{
$customer = $review->customer;
return $this->collection($customer, new CustomerTransformer);
}
The problem is that $customer is an Item an not a Collection. I had to change this->collection to this->item.

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;
}

Call a member function on a non object

i want to display informations about object Activity that the function getCurrent() from the ListActivity should returns.
When i try it, it works perfectly, i have the information needed from the class, but, i have this error message on the top of the page :
Fatal error: Call to a member function getIdentifiant() on a
non-object in
/Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php
on line 34
Line 34 is here :
while($listActivities->next())
{
$current = new Activity();
$current = $listActivities->getCurrent();
echo $current->getId(); // line 34
}
And this is the getCurrent() function which return an Activity object.
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
}
I don't understand why i have this problem since it returns me the object that i want.
Please help me figuring it out. Thanks.
echo $current->getId(); // line 34
Fatal error: Call to a member function getIdentifiant() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/site/prototype/administration.php on line 34
WhatEVER you think happens, or whatEVER you see in your page, if the error says $current is not an object, it is not. It might not be null, but it could also be an array or anything that is not an object.
Also :
$current = new Activity();
$current = $listActivities->getCurrent();
Doesn't really makes sense to me.
Use
var_dump($listActivities->getCurrent());
To see what it exactly returns, and trust what errors say.
EDIT : And you may not even be looking at the right php script acually : The error says "getIdentifiant" while the code says "getId". Make sure you're looking at the right piece of code and refreshing the right page.
first set $this->current after $current = new Activity(); (maybe in constercture)
and you should return false if !isset($this->activities[$this->current])
also if you use $current = $listActivities->getCurrent() you lost your Activity object , it should save into another variable
here new code :
while($listActivities->next())
{
$current = new Activity();
if( $listActivities->getCurrent() )
echo $current->getId(); // line 34
}
public function getCurrent()
{
if(isset($this->activities[$this->current]))
return $this->activities[$this->current];
return false;
}

Categories