Joomla: difference between JFactory::getUser and JUserHelper::getProfile - php

$profile = JFactory::getUser($this->_user_id);
$userDetails = JUserHelper::getProfile($this->_user_id);
$profile->details = isset($userDetails->profile) ? $userDetails->profile : array();
I'd like to know the difference between the two function JFactory::getUser() and JUserHelper::getProfile().
And what do those 3 lines of code do?
An informative answer will be highly appreciated.
Thank you!

While JFactory::getUser() method, returns the global JUser object (from where you can access the JUser methods, the JUserHelper::getProfile() returns the integer for user id. From here, it's your decision to choose what you need in your specific case - if just the use id integer or the JUser object.
$profile->details will not be set as it is not a property of the JUser object. This most certainly will throw you a PHP notice.

Related

Can't access public property?

I have this code retrieved from var_dump($cache) where $cache is the variable holding the object: http://pastebin.com/9Ufpzbdn (Sorry, code is hard to format here but it's not that long).
How do I access property $_redis?
I tried $cache->_redis but it says it's undefined.
I just want to access it so I can use its set and fetch methods.
I have not touched any php for awhile now. Thanks in advance!
I think you can try:
$redis = $cache->getBackend()->_redis;

in() method - Typo3 - queryInterface

if you use the QueryInterface from Typo3 or Flow3 you can look up in the QueryInterface Extbase Dokumentation for all functions you can use. I already created some ANDs, ORs and LogicalNOTs in Flow3 and they work great.
My problem is the in() function. Let's say I have a "task" object and every task has one "status" object (over Many-To-One). Now I want to have all tasks having a status with the 'show' attribute on 'false'. That's what doesn't work:
$query->in('status',$this->statusRepository->findByShow(FALSE));
I guess it's because of the return value types of find(). You can get 'NULL', one object or many objects in an array. But why it doesn't work and how can I fix it?
Thanks for help.
It should work like this (assuming the set of status objects is not empty):
$query = $this->createQuery();
$query->matching($query->in('status', $this->statusRepository->findByShow(FALSE)));
return $query->execute();
When you call findByShow, it return an object of QueryResult, the second parameters in the "in" method should be an array of mixed elements.
Try to use the toArray() method of QueryResult to convert your object into an array of your status model.
$this->statusRepository->findByShow(FALSE)->toArray();
I hope it helped!
Olivier
I'm not sure if they fixed this by now but I remember spending hours last year to find out I had to do this:
$statusIds = Array();
$status = $this->statusRepository->findByShow(FALSE);
foreach($status as $s) $statusIds[] = $status->getIdentifier();
$constraint = $query->in('status',$statusIds);
return $query->matching($constraint)->execute();
Your Status class must implement the following:
public getIdentifier(){ return $this->Persistence_Object_Identifier; }

Where to instantiate objects for a collection if the DataMapper is only for transfer/exchange of data?

Since the DataMapper is supposed to be for the exchange/transfer of data between objects and the relational database I would get a user like this
$user = $this->entityFactory->build('User');
$userMapper = $this->mapperFactory->build('User');
$user->setId(43);
$userMapper->fetch($user);
That works fine because I can create the User object outside of the mapper and pass it in but what do I do when I am getting a collection/list of objects?
Creating the empty objects outside of the mapper first just does not seem correct and would surely cause some problems so what is the best way to do it?
Thanks.
I don't know if this question is still in your mind, however let me give you an answer to this. In principle the first step is the same
$userCollection = $this->entityFactory->build('UserCollection');
$userCollectionMapper = $this->mapperFactory->build('UserCollection');
$user = $this->entityFactory->build('User');
$user->setId(43);
$userCollection->add($user);
$userCollectionMapper->fetch($userCollection);
So the userObject would function here as an searchObject for the collectionMapper (like teresko proposed in an older thread). Your CollectionMapper would retrieve the data from the database and i prefer to use something like
//returns new User object
$newUser = $userCollection->newUser();
//populate user object with DB entry
$userCollectionMapper->populate($newUser,$DBresponse);
//add to collection
$userCollection->add($newUser);
Of course there would be a loop before that looping through the found lines in the database and you would have to clear the list of user objects before adding the results.
So this is the way i would deal with the problem. Hope it helps.

set models based on condition in cakephp queries

This is probably very easy to do, but I can't seem to get my head around it right now. Let's say in a component in a cakephp application, I have a variable my_model, which contains the model of the corresponding controller that is currently using the component like:
function TestComponent extend Object
{
var $my_model; // can be either User, or Person
function test()
{
$myModelTemp = $this->my_model;
$model = $myModelTemp != 'User' ? $myModelTemp.'->User' : 'User';
$this->$model->find('all');
}
}
As you can see above in my function test() what I'm trying to do is call the correct model based on the value of my_model. So based on the condition, my query will be either:
$this->Person->User->find('all');
Or
$this->User->find('all');
When I do it like I did above, I get an error saying Fatal error: Call to a member function find() on a non-object. In order words, that error means Person->User is not an object (so, it is considered as a string).
What you're saying could be true, however, it can refer to any part of the call.
So either Person or User could be invalid, or together they causes the error. Hard to say.
Try dumping the individual objects using var_dump();
So try:
<?php
echo "<pre>";
var_dump(is_object($this->Person));
var_dump(is_object($this->User));
echo "</pre>";
?>
to determine where you're code goes wrong.
To be clear, that return value needs to be true for it to be an object.
The one that returns false is the likely culprit.
Should your question refer to the correct way to reference an object, an object is basically an array. For example:
<?php
$obj = (object) array("this", "my_function");
?>
The above example casts the array as an object. However, using multiple layers might prove to be more difficult than you'd expect.
Generally, it looks like you might be going about this all wrong. Obviously you want the models to be dynamic, but then you're hard-coding things which defeats the whole point of it being dynamic in the first place.
It also seems like you might be violating the principals of CakePHP and MVC by doing all this in a component. I'm not sure this component should really be manipulating models or assuming which models are currently in use.
However, if you want to evaluate a string as an actual object, you can wrap it in { ... } (this is valid standard PHP syntax, not Cake-specific code).
Try this:
$modelName = $this->my_model;
$model = ($modelName != 'User') ? $this->{$modelName}->User : $this->User;
$model->find('all');
Now, if this doesn't work or you get an error saying it can't find the model(s) you need to ensure the models are actually loaded and initialised in the current scope.

Getting database values into objects

The thing is that you have classes and then you have the database data. When you create an object how do you set the objects properties to contain the data in the database ?
I saw something like this and I'm wondering if this is really the best way to do it. I'm sure this is a fairly common issue, but I don't know what are the most accepted solutions on how to handle it.
In this example when the object is created you pass an id as a parameter and then you run a query to the database with the id and you assing the returned values to the object properties. I don't have much PHP experience and haven't seen this used much.
Is this an acceptable way to achieve this purpose ? Is there a better or more accepted way ?
public function __construct($id = null){
if($id != null){
$sql = "SELECT *
FROM users
WHERE user_id = $id";
$res = Db::returnRow($sql);
// $res contains an associative array with database columns and values
if($res){
$this->user_id = $res['user_id'];
$this->user_name = $res['user_name'];
//and so on...
}
}
}
Could somebody provide some sample code or pseudocode to illustrate what is the correct way to do this ?
It could be an acceptable way for a homework maybe. But architecturaly it is not.
Your class that is representing your business data (a user in your example) must be loosely coupled with your database access logic. In the end the PHP class acting as a user should not be aware that the data come from a database, a file or any other resource. Following that you will be able to reuse your user php class in other projects without having to change anything to it! If you have your data access logic inside it you are stuck.
Conclusion: I would suggest to read some resources on Design Pattern (in your situation take a look at DAO pattern) ;) Hint: the one from Head First series is extremely accessible and enjoyable.
You could create a function to do this for you automatically, by looping over the associative array's key/value pairs. Or you could look into using an ORM library.
Yes, you can semi-automate this by having a parent class all objects inherit from. On load, it queries, "SHOW FIELDS FROM [my tablename]" and populates an associative array with the names. If an id has been passed in, it looks for a valid object in that table with that id and assigns the values to the array.
Side note: don't pass your id directly into your query like that. Parametize the sql and wrap a function around any user input to sanitize it.
If it's mysql, you can just do:
$obj = mysql_fetch_object($query);
PDO the ability to use arbitrary classes as the target for a fetch, but beware that they assign the variable data before running the constructor:
$pdo->query($stmt, PDO::FETCH_CLASS, "MyClass", array('foo'=>'bar'));
...where the final parameter contains arguments for your class constructor.

Categories