How to attach files to user by code ?
Forum rules
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
Always add your Laravel, Aimeos and PHP version as well as your environment (Linux/Mac/Win)
Spam and unrelated posts will be removed immediately!
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
How to attach files to user by code ?
Hi, guys! While I am going deeply into Aimeos i need more custom things to do. Here I need to ask you, probably it will be faster than I figure out it by myself. Also, I hope, my questions will be useful for somebody.
My environment is the following:
Laravel Framework 5.8.26
aimeos-laravel 2019.04
PHP Version 7.2.19-1+ubuntu18.04.1+deb.sury.org+1
How can I attach several files to specific user by code ? I don't need attach uploaded files, but I need attach some existing files from filesystem. How can I do it ? In database I see mshop_media table, so it must be possible. Can you, please, provide me example code ?
Thank you a lot!
My environment is the following:
Laravel Framework 5.8.26
aimeos-laravel 2019.04
PHP Version 7.2.19-1+ubuntu18.04.1+deb.sury.org+1
How can I attach several files to specific user by code ? I don't need attach uploaded files, but I need attach some existing files from filesystem. How can I do it ? In database I see mshop_media table, so it must be possible. Can you, please, provide me example code ?
Thank you a lot!
Re: How to attach files to user by code ?
If this is releated to your former post, please have a look at
laravel-package-f18/how-to-implement-ne ... t2103.html
For those who want to know how to do it by hand:
laravel-package-f18/how-to-implement-ne ... t2103.html
For those who want to know how to do it by hand:
Code: Select all
$mediaItem = \Aimeos\MShop:create( $context, 'media' )->createItem();
$mediaItem->setUrl( 'relative/path/to/file' )->setPreview( 'relative/path/to/file' );
$mediaItem = $manager->saveItem( $mediaItem );
$manager = \Aimeos\MShop:create( $context, 'customer' );
$customerItem = $manager->getItem( '<customer id>', ['media] );
$listItem = $manager->createListItem()->setRefId( $mediaItem->getId() );
$customerItem->addListItem( 'media', $listItem );
$manager->saveItem( $customerItem );
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: How to attach files to user by code ?
I am very thankful for your help!
But I am trying to attach file to the user and I am getting error, the main code looks like:
And the error is:
Thank you a lot for the help!
But I am trying to attach file to the user and I am getting error, the main code looks like:
Code: Select all
$context = $this->getLaravel()->make( 'Aimeos\Shop\Base\Context' )->get( false, 'command' );
$context->setEditor( 'aimeos:account' );
$localeManager = \Aimeos\MShop::create( $context, 'locale' );
$localeItem = $localeManager->bootstrap( 'default', '', '', false );
$context->setLocale( $localeItem );
$groupManager = \Aimeos\MShop::create( $context, 'customer/group' );
$customerListsManager = \Aimeos\MShop::create( $context, 'customer/lists' );
$mediaManager = \Aimeos\MShop::create( $context, 'media' );
$customerManager = \Aimeos\MShop::create( $context, 'customer' );
$editorGroupId = $groupManager->findItem( 'editor' )->getId();
$mediaItem = \Aimeos\MShop::create( $context, 'media' )->createItem();
$mediaItem->setUrl($filePath);
$mediaItem->setType('default');
$mediaManager->saveItem($mediaItem);
$customerItem = $customerManager->getItem( $user->id, ['media'] );
$listItem = $customerManager->createListItem()->setRefId( $mediaItem->getId() );
$customerItem->addListItem( 'media', $listItem );
$mediaManager->saveItem( $customerItem );
And the error is:
Code: Select all
Aimeos\MShop\Exception : Unable to call method "createListItem"
Re: How to attach files to user by code ?
Sorry, it's "createListsItem()"
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: How to attach files to user by code ?
Hmm, I have changed the code:
But I still get error:
By the way, this code I try in Laravel command class.
I really need help (
Code: Select all
$listItem = $customerManager->createListsItem()->setRefId( $mediaItem->getId() );
Code: Select all
Aimeos\MShop\Exception : Unable to call method "createListsItem"
at /var/www/labor/vendor/aimeos/aimeos-core/lib/mshoplib/src/MShop/Common/Manager/Base.php:52
48| * @throws \Aimeos\MShop\Manager\Exception If method call failed
49| */
50| public function __call( $name, array $param )
51| {
> 52| throw new \Aimeos\MShop\Exception( sprintf( 'Unable to call method "%1$s"', $name ) );
53| }
54|
55|
56| /**
Exception trace:
1 Aimeos\MShop\Common\Manager\Base::__call("createListsItem", [])
/var/www/labor/vendor/aimeos/aimeos-core/lib/mshoplib/src/MShop/Common/Manager/Decorator/Base.php:51
2 call_user_func_array([])
/var/www/labor/vendor/aimeos/aimeos-core/lib/mshoplib/src/MShop/Common/Manager/Decorator/Base.php:51
Please use the argument -v to see more details.
I really need help (
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: How to attach files to user by code ?
I have fixed error and seems media is adding correctly. The code is:
But now problem is that I can't display all media of current logged in user.
I have the code:
$view = $this->getView();
$context = $this->getContext();
but if I make dump of single item I see the properties:
Here I don't see any user or userId field. Or I don't know how it works ? Please let me know how can I get all media of current logged in user ?
Thank you!
Code: Select all
/** @var \Aimeos\MShop\Media\Item\Standard $mediaItem */
$mediaItem = \Aimeos\MShop::create( $context, 'media' )->createItem();
$mediaItem->setUrl($filePath);
$mediaItem->setType('default');
$mediaManager->saveItem($mediaItem);
$customerItem = $customerManager->getItem( $user->id, ['media'] );
$listItem = $cntl->createListItem()->setRefId( $mediaItem->getId() );
$listItem->setType('default');
$customerItem->addListItem( 'media', $listItem );
$customerManager->saveItem( $customerItem );
I have the code:
$view = $this->getView();
$context = $this->getContext();
Code: Select all
try
{
$manager = \Aimeos\MShop::create( $context, 'media' );
$search = $manager->createSearch();
$total = 0;
$view->items = $manager->searchItems( $search, [], $total );
$view->total = $total;
}
but if I make dump of single item I see the properties:
Code: Select all
-values: array:14 [▼
"media.id" => "43"
"media.siteid" => "1"
"media.languageid" => null
"media.type" => "default"
"media.url" => "some/path"
"media.label" => null
"media.status" => "1"
"media.mimetype" => null
"media.domain" => null
"media.preview" => null
"media.mtime" => "2019-07-16 08:43:52"
"media.editor" => "aimeos:account"
"media.ctime" => "2019-07-16 08:43:52"
"languageid" => "en"
]
Here I don't see any user or userId field. Or I don't know how it works ? Please let me know how can I get all media of current logged in user ?
Thank you!
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: How to attach files to user by code ?
Also I have figured out, that products have 'Product Lists Types' and there is 'media' type! Seems I have to add media type for users ? Because I attached files to user but I can't get them! I can't get files of specific user. I see only 'group' list type for user
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: How to attach files to user by code ?
Hmmmm(
I have added Customer Lists Type for customer - media.
when I add media to user entities in DB are created:
users_list table:
https://prnt.sc/ofsujj
mshop_media table:
https://prnt.sc/ofsvba
But media doesn't appear in the customer media tab!
https://prnt.sc/ofsw0c
When I dump my \Aimeos\MShop\Customer\Item\Standard object, there I have the following thing:
But I think there also must be "customer/media"
I did it like admin said:
I have added Customer Lists Type for customer - media.
when I add media to user entities in DB are created:
users_list table:
https://prnt.sc/ofsujj
mshop_media table:
https://prnt.sc/ofsvba
But media doesn't appear in the customer media tab!
https://prnt.sc/ofsw0c
When I dump my \Aimeos\MShop\Customer\Item\Standard object, there I have the following thing:
Code: Select all
-listItems: array:1 [▼
"customer/group" => array:1 [▶]
]
I did it like admin said:
aimeos wrote: ↑15 Jul 2019, 13:01 The easiest way is to copy the product media subpanel (class + template) and rename every occurence of "Product" to "Customer" and "product" to "customer". Then, add "media" to the list of subparts in the customer panel configuration:
https://github.com/aimeos/ai-admin-jqad ... dm.php#L61
That should be all you need to get the media tab into the customer panel. The customer panel lists all customers if you haven't restricted it to the current logged in user ( $context->getUserId() should to the trick). For the customer, in the detail view only his media files will be listed in the new tab.
The manager code you mention can be used to retrieve media items from the database manually like this:The initCriteria() method only sets the conditions and the sorting automatically depending on the given parameters.Code: Select all
$manager = \Aimeos\MShop::create( $context, 'media' ); $search = manager->createSearch(); $total = 0; $view->items = $manager->searchItems( $search, [], $total ); $view->total = $total;
Re: How to attach files to user by code ?
If you copy&paste the panel, you have to add "media" in this line (or the configuration) to retrieve your media items:
https://github.com/aimeos/ai-admin-jqad ... d.php#L441
https://github.com/aimeos/ai-admin-jqad ... d.php#L441
Professional support and custom implementation are available at Aimeos.com
If you like Aimeos, give us a star
If you like Aimeos, give us a star
-
- Expert
- Posts: 274
- Joined: 27 Jun 2019, 16:19
Re: How to attach files to user by code ?
Ahhhhh! )) I have figured out it 3 minutes before you answered) Thank you a lot !
But is there a difference about clashes ? Should I use domains with slashes like this ?
As you see I added just 'media', but not 'customer/media'
But is there a difference about clashes ? Should I use domains with slashes like this ?
Code: Select all
$domains = ['customer/address', 'customer/group', 'customer/property', 'media'];