here is my little story: I am using DoctrinExtensions Tree on my entity account. The user can edit the tree in the UI and then save it. I send back back to the PHP an array of all the accounts. Then I want to rebuilt it as a tree and save/edit the accounts with the methods of the extension.
So I want to compare the UI returned array with my original tree from the db. I get the data by doing something like this:
$repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
$arrayTree = $repo->findAll();
So I have my tree in an array. What I want now is to find an object within this array by itsd ID. I know how to write the function, but in the MVC I don't know what's the right place to write it and call it from, nor if it's the correct way.
I tried to create a folder "Model" and a file Functions.php like this:
namespace NRtworks\ChartOfAccountsBundle\Model;
function get_account_from_id($array)
{
return "true";
}
and then call it from my controller
use NRtworks\ChartOfAccountsBundle\Model\Functions;
get_account_from_id($arrayTree);
But that doesn't work. Please advise on how I should do this and if there is a more correct way in the scope of the MVC idea.
thanks
You should write custom service and put logic inside. Doc: http://symfony.com/doc/current/book/service_container.html#what-is-a-service
UPDATE (Code example):
Les's configure service in the Container:
# app/config/config.yml
services:
your_service:
class: NRtworks\ChartOfAccountsBundle\Service\YourService
Now, your service class:
namespace NRtworks\ChartOfAccountsBundle\Service;
class YourService {
public function getAccountFromId(array $array)
{
return "true";
}
}
Now you could get this service from container like:
class SomeController extends Controller {
public function someMethod() {
$yourService = $this->get('your_service');
}
}
You can even inject your repository class to this service, like:
# app/config/config.yml
services:
app.accounTtree.repository:
class: Doctrine\ORM\EntityRepository
factory-service: doctrine.orm.entity_manager
factory-method: getRepository
arguments:
- "App\MainBundle\Entity\Gallery"
your_service:
class: NRtworks\ChartOfAccountsBundle\Service\YourService
calls:
- [ setRepository, ["@app.accounTtree.repository"]]
and just modify your service:
namespace NRtworks\ChartOfAccountsBundle\Service;
class YourService {
protected $repository;
public class setRepository($repository) {
$this->repository = $repository;
}
public function getAccountFromId(array $array)
{
return "true";
}
}
If we are talking about custom data handling fetched from doctrine, than maybe it is better to use Repository Class. It is designed for such purpose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.