0

ここに私の小さな話があります: 私は自分のエンティティ アカウントで DoctrinExtensions Tree を使用しています。ユーザーは、UI でツリーを編集して保存できます。すべてのアカウントの配列を PHP に送り返します。次に、ツリーとして再構築し、拡張機能のメソッドを使用してアカウントを保存/編集します。

だから私はUIが返した配列をdbからの元のツリーと比較したい. 次のようなことをしてデータを取得します。

$repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
$arrayTree = $repo->findAll(); 

だから私はツリーを配列に持っています。私が今欲しいのは、その ID によってこの配列内のオブジェクトを見つけることです。関数の書き方は知っていますが、MVC では、関数を記述して呼び出すのに適切な場所は何か、またそれが正しい方法であるかどうかもわかりません。

次のように、「Model」フォルダーとファイル Functions.php を作成しようとしました。

 namespace NRtworks\ChartOfAccountsBundle\Model;

 function get_account_from_id($array)
 {
    return "true";    
 }

そして、コントローラーから呼び出します

use NRtworks\ChartOfAccountsBundle\Model\Functions;
get_account_from_id($arrayTree);

しかし、それはうまくいきません。これをどのように行うべきか、またMVCのアイデアの範囲内でより正しい方法があるかどうかについてアドバイスしてください。

ありがとう

4

2 に答える 2

0

カスタム サービスを作成し、内部にロジックを配置する必要があります。ドキュメント: http://symfony.com/doc/current/book/service_container.html#what-is-a-service

更新 (コード例):

コンテナ内の Les configure サービス:

# app/config/config.yml
services:
    your_service:
        class:        NRtworks\ChartOfAccountsBundle\Service\YourService

さて、あなたのサービスクラス:

namespace NRtworks\ChartOfAccountsBundle\Service;

class YourService {
    public function getAccountFromId(array $array)
    {
        return "true";
    }
}

これで、次のようなコンテナーからこのサービスを取得できます。

class SomeController extends Controller {
    public function someMethod() {
        $yourService = $this->get('your_service');
    }
}

次のように、リポジトリ クラスをこのサービスに注入することもできます。

# 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"]]

サービスを変更するだけです:

namespace NRtworks\ChartOfAccountsBundle\Service;

class YourService {
    protected $repository;

    public class setRepository($repository) {
        $this->repository = $repository;
    }

    public function getAccountFromId(array $array)
    {
        return "true";
    }
}
于 2014-03-09T22:44:39.397 に答える