model example



    /** * Override save method to make use of symfony form and custom data mapping * If isDefaultBillingAddress or isDefaultShippingAddress, the appropriate action will be made */
    public function save($data)
    {
        if (!empty($data['id'])) {
            $model = $this->getRepository()->find($data['id']);
        } else {
            $model = new $this->model();
            $this->getManager()->persist($model);
        }

        $data['country'] = $data['countryId'];
        $data['state'] = $data['stateId'] ?? null;

        $form = $this->get('shopware.form.factory')->create(AddressFormType::class$model);
        $form->submit($data);

        if ($form->isSubmitted() && !$form->isValid()) {
            $errors = [];
            

    public function setModel($which = null)
    {
        if ($which) {
            $this->model     = is_object($which) ? $which : null;
            $this->modelName = is_object($which) ? null : $which;
        }

        if (empty($this->model) && ! empty($this->modelName) && class_exists($this->modelName)) {
            $this->model = model($this->modelName);
        }

        if (empty($this->model) && empty($this->modelName)) {
            $this->modelName = get_class($this->model);
        }
    }
}

    public function __construct($model, ?array $formatters = null, ?string $locale = null)
    {
        if (is_string($model)) {
            // Create a new model instance             $model = model($model, false);
        }

        if (is_object($model)) {
            throw new InvalidArgumentException(lang('Fabricator.invalidModel'));
        }

        $this->model = $model;

        // If no locale was specified then use the App default         if ($locale === null) {
            $locale = config(App::class)->defaultLocale;
        }

    public function save($data)
    {
        $model = $this->getRepository()->find((int) ($data['id'] ?? 0));
        if ($model === null) {
            $model = new $this->model();
            $this->getManager()->persist($model);
        }

        $data = $this->resolveExtJsData($data);
        $model->fromArray($data);

        $violations = $this->getManager()->validate($model);
        $errors = [];
        foreach ($violations as $violation) {
            $errors[] = [
                'message' => $violation->getMessage(),
                
Home | Imprint | This part of the site doesn't use cookies.