I am trying to load in Entity classes and use within a loop in order to load content in dynamically from files into relating tables.
Is there any way i can load in all Entity files from the following
use AppBundle\Entity\aaPostcode;
use AppBundle\Entity\abPostcode;
use AppBundle\Entity\acPostcode;
use AppBundle\Entity\adPostcode;
in such a way like this?
use AppBundle\Entity\*
Not sure if this is possible in Symfony.
My next issue is using the the prefixedEntity within a loop like so -
new $entityPrefix
When i am setting $entityPrefix to the following format
$entityPrefix = str_replace([".csv"], "", $entityFilename) . "Postcode" . '()';
which returns the string of
"abPostcode()"
can anyone advise as to why calling
new $entityPrefix;
is not working
Thanks in advance for any help!
trying to call
new $entityPrefix();
returns
[Symfony\Component\Debug\Exception\ClassNotFoundException]
Attempted to load class "abPostcode" from the global namespace.
Did you forget a "use" statement for "AppBundle\Entity\abPostcode"?
even when i current am hrd coding the use stament to call
use AppBundle\Entity\abPostcode;
You can't instanciate dynamically your class without ginving the full namespace. Try that :
$namespace = "AppBundle\Entity\\";
$entityName = "YourEntity";
$namespace .= $entityName;
$class = new $namespace();
This is working for me...
I solved this by pulling out the functionality into a helper and running a switch statement based on the input - in this case a {prefix}
I kinda ran into this issue migrating data from one database type to another. IE: Oracle to MySQL. With hundreds of tables the use section would get big.
This is on Symfony 5.2.x BTW.
When I run a query using a standard repository or a repository linked to the table I can call the entity with its full namespace.
$oracle = $em2->getRepository(\App\Entity\Oracle\Appoints::class)->Appoints();
So I did not have to load "use App\Entity\Oracle\Appoints
Then I can create a new object in the same way:
$mysqlObject = new \App\Entity\OracleAppPoints();
Related
I have a little problem in here. I'm new at cakephp and now have to developing a cakephp shell script to saving data into its database. The problem is, I work on default environment and need to save data into another environment. I'm using this code to switch environment:
ConnectionManager::alias($env, 'default');
It seems good since I've got right output when try to get database.
$this->out($datasource->config()['database']);
And then I load my model:
$model = $this->Model;
But It's load the model data from default environment. Is my approach wrong? Or, there is another method to switch environment on the go with cakephp?
That should work just fine, and a quick test shows that it does. You'd probably have to show a little more context, but I guess you are loading the model (what you're showing there isn't loading but accessing) before the connection alias is being created, hence the model will use the original connection that it received when it has been instantiated.
So either make sure that you load the model afterwards, respectively that you create the alias before the model is being loaded (that is when TableRegistry::get() is being invoked), or change the connection of the specific model on the fly in case applicable:
$connection = ConnectionManager::get($env);
$model->setConnection($connection); // use connection($connection) in CakePHP < 3.4
I use MongoDB PHP library in the client object I can see the method for drop a database, how can I create a new database?
You probably have problems with the naming.
In mongodb a DB is called an Index and a tabe called a Collection.
You will find createIndex in the docs:
http://mongodb.github.io/mongo-php-library/api/namespace-MongoDB.Operation.html
https://docs.mongodb.org/v3.0/reference/method/db.collection.createIndex/
the namespace database is automatically create if it does not exist when we create collection
use MongoDB\Driver\Manager;
use MongoDB\Database;
$manager = new Manager("mongodb://localhost:27017");
$database = new Database($manager, 'newDataBase');
var_dump($database->createCollection('CollectionName'));
sorry i keep learning
In my Laravel 5 app, I'm implementing Transformers and Fractal.
I've got in my example two different models: User and UserLogin. Every User can have multiple UserLogins (I've already added a one-to-many relationship between them). Now I want to "clean" my response, which returns an User with his UserLogins. So I've created two transformers, and I thought that I should call a transformer inside the other one inside his return, like here:
"UserLogins"=> Fractal::collection($user->userLogins, new UserLoginTransformer).......
Unfortunately it doesn't work and the error is that it doesn't find fractal library (which is correctly imported).
What could be the problem?
Finally found the solution.
Fractal class does not exists, I cannot make simplier than that.
And you weren't correctly using the library.
So, the solution :
use \League\Fractal\Manager;
use \League\Fractal\Resource\Collection as FractalCollection;
$fractal = new Manager();
$resource = new FractalCollection($user->userLogins, new UserLoginTransformer);
return $fractal->createData($resource)->toArray();
Here's a pickle :)
In the old Yii you could instantiate models anywhere in protected. Regardless of the module you were in. Then the Yii team decided to go and mess everything up :) and decided to change the structure and code and now, I'm a bit lost...
1. HOW DO YOU INSTANTIATE MODELS? (anywhere in the project)
Old fashion way was $model = new Model(); where model could be in a totally different module and it would still work. How do we do this now? when I try to do it, it says: Class 'app\modules\somemodule\controllers\Model' not found which is funny because I want a model and it searches in controllers...
2. SESSIONS IN YII
Old fashion way was
Yii::app()->session['var'] = 'value';
echo Yii::app()->session['var']; // Prints "value"
How are they done now?
L.E: Found my answer to the second question :D and sessions are handled about the same: Yii::$app->session['var'] = 'value'; the difference being the $... It's all about the $ :)
Thank you!
Ares.
Funny how no one showed interest in my question...
Anyways, nothing changed about it. If you need new fresh instance it's just "new Post()". If you need to get AR model with data it's "Post::find->where(...)->one()"
BUT you have to:
Either import class from another namespace with use:
use app\modules\someModule\models\Post;
// ...
$post = new Post();
or use fully qualified class name:
$post = new \app\modules\someModule\models\Post();
Hope this helps others as well as it did me :D
I'm using a really basic library in Codeigniter. In order to use it I need to pass in a number of config parameters using a config function. My library currently requires me to instantiate it before I can call the config, i.e., I have to use it as below:
$this->load->library('Library');
$instance = new Library();
$instance->config($configparams);
I'd like to use it like standard CodeIgniter libraries:
$this->load->library('Library');
$this->library->config($configparams);
What do I need to add to the library in order to have it auto-instantiate? The code is as below:
class Library {
function config($configparams){
...
}
}
This is working now. I swear it wasn't working before I posted on SO! Thanks for posts.
Once you load a class
$this->load->library('someclass');
Then when use it, need to use lower case, like this:
$this->someclass->some_function();
Object instances will always be lower case
According to the docs, you should just call it. So:
$this->load->library('Library');
$this->library->config($configparams);
But why not just pass $configparams to the constructor:
$this->load->library('Library', $configparams);
Check out the guide for CodeIgniter -- it's a great resource to learn more about the framework. IMHO, there aren't any good books available on the current version; this is it.
You basically call it like anything else.
$this->load->library('Name of Library')
Read more here: http://www.google.com/url?sa=t&source=web&cd=2&ved=0CCIQFjAB&url=http%3A%2F%2Fcodeigniter.com%2Fuser_guide%2Fgeneral%2Fcreating_libraries.html&ei=tLFUTbz3HI3SsAOYgP2aBg&usg=AFQjCNFo751PYFp5SbqzuZMxGhXwMI8SJA