How do i use findAll method with a class in Doctrine? - class

I am a little bit confused with Doctrine class. I created a class and its base class in Doctrine. The class's name is User.
so..I created an object of class User.
$oUser = new User();
when I try to use the findAll method, it does not work. I found the following code on the doctrine documentation:
Doctrine_Core::getTable('User')->findAll();
I don't understand why i need to call getTable to use the findAll method when I have User class.
Am I missing something?

For Doctrine 2, you need to get the repository:
$AllUsers = EM()->getRepository('Users')->findAll();

AFAIK User object represents a single row in a table.if you need all the users you need to ask the table.

Related

Add entity to the context without knowing the type

Is it possible to add a POCO entity to the object context without knowing the type?
For example, here I'm adding an employee...
context.Employ.Attach(employer);
Is it possible to do something like this...
context.Attach(employer);
This works...
context.Set(entity.GetType()).Attach(entity);
You may be able to use the Set property of your context. For example:
var entityType = employer.GetType();
context.Set(entityType).Attach(employer);

Do action right after entity created in SonataAdmin

is there any way that I could config my SonataAdmin to do action right after the default CRUD?
The situation is that, I've got some classes, each class has a BCode, which must be created with the entity. BCode is a tweaked crc32 string. so I need a customized action to be able to create this code with entity.id
thanks in advance
Your admin class extends the vendor/sonata-project/admin-bundle/Admin/Admin.php so you just have to implement the methods postPersist and postUpdate.
If you use doctrine as ORM you can also use the doctrine events postUpdate and postPersist as described in the documentation.

Extbase Repository objectType = NULL

We are migrating a 4.5 Extension to 7.2. One special case is strange. Trying to get a findOneByUid brings a "No class name was given to retrieve the Data Map for." Error.
Accessing via another object and using the DebuggerUtility it allows us to navigate to the object that fails, and there we can see, the objectType is NULL.
Any clue where to search? All the other objects can be accessed via findOneByUid.
How would you proceed to find the issue?
Adding the following lines solved the problem... any idea how to avoid this?
public function __construct() {
$this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$this->objectType = \TYPO3\CMS\Core\Utility\ClassNamingUtility::translateRepositoryNameToModelName($this->getRepositoryClassName());
}
The object type can only be null if the constructor of the repository has been overridden in a subclass without a call to the parent constructor. parent::__construct();
Instead of using the constructor, you should make use of the method initializeObject, which gets called after the constructor and which can safely be overridden.

MagicalRecord: How to import values into a related entity

I'm using a web service which returns results like the following example:
{
"name":"Frank",
"meals":[
"cheeseburger",
"lasagne"
]
}
My Core Data schema looks like this:
Using MagicalRecord's MR_importValuesForKeysWithObject method, how would I set about mapping the meals key to the related Meals.name attribute?
I can map the meals manually after, using a for in loop, but just wondered if there was a way MR_importValuesForKeysWithObject would perform this for me?
Basically I want each object in the JSON "meals" array to become a new Meals entity.
Override importMeals: on the Person object and do the lookup/create/associate manually.
(longer answer)
every property imported via MagicalRecord calls import(PropertyName) on the target object, by implementing it you can override functionality.

Magento: Accessing models/blocks from a phtml

Hi I have a situation where I need to look up the number of recently viewed products on catalog/product/view.phtml. In the recently viewed 'product_viewed.phtml' file it calls
$_products = $this->getRecentlyViewedProducts()
to get the recently viewed. How would I access this method from within the catalog/product/view.phtml file?
I don't know where this method is. I've tried searching for it but it doesn't seem to exist. When I write click it in Netbeans and click go to declaration it takes me to
class Mage_Reports_Block_Product_Viewed extends Mage_Reports_Block_Product_Abstract
Actually on the class itself. This class only has _toHtml(), getCount(), and getPageSize() methods.
I just need to know whether there are any recently viewed products.
Any help most appreciated!
Billy
If you look into 'Mage_Reports_Block_Product_Viewed', you will notice:
$this->setRecentlyViewedProducts($this->getItemsCollection());
That 'getItemsCollection' method is defined in the abstract class... And you will notice this abstract class will create a model based on $_indexName defined in the (subclassed) block.
If you just want the collection, you can probably get away with:
$_products = Mage::getModel('reports/product_index_viewed')->getCollection();
And then adding whatever you want to the collection:
$_products
->addAttributeToSelect('*')
->setAddedAtOrder();
// optionally add other methods similar to Mage_Reports_Block_Product_Abstract::getItemsCollection
Another approach that might be more suited would be to create the original block:
$productViewedBlock = $this->getLayout()->createBlock('reports/product_viewed');
On which you can simply call whatever you want:
$_collection = $productViewedBlock->getItemsCollection();
$_count = $productViewedBlock->getCount();
The getRecentlyViewedProducts function is a magical getter that gets the data that was set with setRecentlyViewedProducts in app/code/core/Mage/Reports/Block/Product/Viewed.php (which builds it using app/code/core/Mage/Reports/Block/Product/Abstract.php's function _getRecentProductsCollection).
This is complicated stuff that you don't want to reproduce; its better, IMO to make your own Block that extends Mage_Catalog_Block_Product_Abstract that will give you access to the same functionality, and drop your new block into the page you're working on.