eZ Platform Discussions

Create URL ValueObject programmatically


#1

Hi,

I just want to create an eZ\Publish\API\Repository\Values\URL\URL object (Store in ezurl table in DB) in a command.

How to do that?


#2

I’m not near a computer right now, so I can’t give you a piece of working code, but there is an URL service available in the repository as of eZ Platform 2.0 which you can use to create new URLs.

In v1, I don’t think it can be done.


#3

In V1 URLService only expose createUpdateStruct() and updateUrl() functions … (and some find*(), * load*())

So I’ll deal with this problem when I get the exact version of eZ for my project.
(Pro version in contractual terms)

Merci,


#4

Hi,

I have my instance ezplatform enterprise edition that works :slight_smile:
But the URLService does not have more features than before …

URLs are ValueObject. There may be a generic service to create a ValueObject?

class URL extends ValueObject

#5

This is the wrong solution.

    try {
        return $this->repository->getURLService()->loadByUrl($url);
    } catch (NotFoundException $e) {
        $this->repository->beginTransaction();
        try {
            /** @var \Doctrine\DBAL\Connection $connection */
            $connection = $this->container->get('database_connection');
            $connection->exec("INSERT INTO ezurl (url) VALUES ('$url'); ");
            $urlObject = $this->repository->getURLService()->loadByUrl($url);
            $struct = $this->repository->getURLService()->createUpdateStruct();
            $struct->url = $url;
            $urlObject = $this->repository->getURLService()->updateUrl($urlObject, $struct);
            $this->repository->commit();
        } catch (\Exception $e) {
            $this->repository->rollback();
            throw $e;
        }
    }
    return $urlObject;