[TYPO3-english] How to instance the model by code in Typo3 6.2.15

Christian Platt christian.platt at pharmaline.de
Mon Nov 9 16:36:29 CET 2015


Hi stephan,

You saved my day! Thanks a lot. Now for all the others, getting startet and warm with extbase:

What did i wrong? Not to much, the name of my extenion „hockey“ has to be in CamelCase, so „Hockey“ is the correct way to call it.

The second error was that i had the „Classes“ in my namespace

whereas namespace in that model is "namespace Pharmaline\Hockey\Domain\Model;"

So that did not work
$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
			$newHockeyArea = $objectManager->get('Pharmaline\\hockey\\Classes\\Domain\\Model\\HockeyArea');


Now, just correct the two mistakes and it works

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
$newHockeyArea = $objectManager->get('Pharmaline\\Hockey\\Domain\\Model\\HockeyArea');

the other way of syntax i tested is using the „use“ statement in my controller, just below the namespace to use later on the shortCut (alias) of the namespace…. That also did work.

<?php
namespace Pharmaline\Hockey\Controller;

use Pharmaline\Hockey\Domain\Model\HockeyArea as HockeyArea;
…

$newHockeyArea = $objectManager->get(HockeyArea::class);


So i guess, i will be able to open other repositories and write to them. Thanks a lot!

best regards
Christian



> Am 09.11.2015 um 10:37 schrieb Stephan Schuler <Stephan.Schuler at netlogix.de>:
> 
> Signierter PGP-Teil
> Hey there.
> 
> If you need advanced extbase mechanisms when creating an object (dependency extension, initializeObject() calls), the object manager is the way to go. In general when being in extbase environments, the object manager clearly is what you want to use.
> But besides not initializing objects correctly: Both, the object manager as well as GeneralUtility::makeInstance() should at least find your class and create an object.
> 
> There are three things that come to my mind directly:
> * The „Classes“ fragment is not part of your namespace but only the folder inside your extension where to look at for classes.
> * Your extension name should start with an upper case, so „Hockey“ it is.
> * So you did call your extension „hockey“?
> 
> If you’re on an advanced PHP version (Well. Advanced in terms of TYPO3 requirement, not advanced in terms of PHP in general):
> In PHP 5.5 there’s the „::class“ constant. So you might try this:
> 
> $hockeyArea = $objectManager->get(\Pharmaline\Hockey\Domain\Model\HockeyArea::class);
> 
> This even works with the „use“ statement, which results to this:
> 
> use Pharmaline\Hockey\Domain\Model\HockeyArea HockeyArea
> $hockeyArea = $objectManager->get(HockeyArea::class);
> 
> Regards,
> 
> 
> 
> Am [DATE] schrieb "typo3-english-bounces at lists.typo3.org im Auftrag von Christian Platt" <[ADDRESS]>:
> 
> >Hi folks,
> >
> >
> >i am trying to import data from xml source into my database.
> >So i created a model to import the data. So i need an instance of my data model
> >
> >
> >
> >Ín 4.7 extbase i used something like:
> >
> >$newEntry = t3lib_div::makeInstance('Tx_Phlgallery_Domain_Model_Gallery');
> >
> >to get en instance from the data model. In 6.2. this does not work anymore. I searched and found, that...
> >
> >$newHockeyArea=\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Pharmaline\\hockey\\Classes\\Domain\\Model\\HockeyArea');
> >
> >should do the job, but i get
> >Fatal error: Class 'Pharmaline\hockey\Classes\Domain\Model\HockeyArea' not found in /Library/WebServer/Documents/typo62shop/typo3_src-6.2.15/typo3/sysext/core/Classes/Utility/GeneralUtility.php on line 4434
> >
> >i searched further and found that the object manager should do the job
> >
> >$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
> >
> >$newHockeyArea = $objectManager->get('Pharmaline\\hockey\\Classes\\Domain\\Model\\HockeyArea');
> >
> >With that code it also does not work, i get
> >
> >1289386765: Could not analyse class:Pharmaline\hockey\Classes\Domain\Model\HockeyArea maybe not loaded or no autoloader? (More information <http://typo3.org/go/exception/CMS/debug/1289386765>)
> >
> >so, it looks for me that the Model in not found??
> >
> >What is wrong? Normaly i would get the properties of my Domain  Modell, fill it an add it to my repository….
> >
> >
> >Christian Platt
> >
> >
> >
> >
> 
> Stephan Schuler
> Web-Entwickler | netlogix Web Solutions
> 
> Telefon: +49 (911) 539909 - 0
> E-Mail: Stephan.Schuler at netlogix.de
> Web: websolutions.netlogix.de
> 
> 
> 
> 
> netlogix GmbH & Co. KG
> IT-Services | IT-Training | Web Solutions
> Neuwieder Straße 10 | 90411 Nürnberg
> Telefon: +49 (911) 539909 - 0 | Fax: +49 (911) 539909 - 99
> E-Mail: info at netlogix.de | Web: http://www.netlogix.de
> 
> netlogix GmbH & Co. KG ist eingetragen am Amtsgericht Nürnberg (HRA 13338)
> Persönlich haftende Gesellschafterin: netlogix Verwaltungs GmbH (HRB 20634)
> Umsatzsteuer-Identifikationsnummer: DE 233472254
> Geschäftsführer: Stefan Buchta, Matthias Schmidt
> 
> 
> 
> 
> _______________________________________________
> TYPO3-english mailing list
> TYPO3-english at lists.typo3.org
> http://lists.typo3.org/cgi-bin/mailman/listinfo/typo3-english



More information about the TYPO3-english mailing list