I want to create new product in Sylius. I just tested:
$user = $this->getUser();
$repository = $this->container->get('sylius.repository.product');
$manager = $this->container->get('sylius.manager.product'); // Alias for appropriate doctrine manager service.
$product = $repository->createNew();
$product
->setName('Test product')
->setDescription('Des Product 2')
->setPrice(90)
->setUser($user)
;
$manager->persist($product);
$manager->flush(); // Save changes in database.
But it trigger an exception of slug. When I try $product->getSlug(), that returns empty. I don't know how does slug created in Sylius and where is the code for that?
It's a gedmo:slug, check ProductBundle\Resources\config\doctrine\model\ProductTranslation.orm.xml
Make sure you have a default locale configured and add this to your code:
$product->setCurrentLocale($locale);
$product->setFallbackLocale($locale);
Products are translatable, and I think this is what you're missing.
Check how a product is created in Sylius\Bundle\FixturesBundle\DataFixtures\ORM\LoadProductsData line 404.
Related
Can I get some help? I am trying to query my model database as a search. When the results are fetched, I want to pass the id of the collection through a form into another model and I don't want to use a package.
So, far, I have been getting errors.
Basically, I am querying the database with
$services = Service::where('name' 'LIKE' Request::('input')):
But if I try to pass to pass $servces to view with
return view('search.result')->with($services);
It returns **Illegal Offset Type. **
So, instead I have to chain the $services in details
Like so:
return view('search.results')->withDetails($services);
On my results.blade.php, I have:
#foreach($details as $d)
{{$d->name}}
<form action="{{route('book', ['id' =>$d->id} method="POST">
<button type="submit">Book</button>
</form>
#endforeach
This displays the services collections
But, if I try to pass to pass the Id of $d collection into the Book model like this:
public function book($id) {
$service = Service::find($id);
//or
$service = Service::where('id', $id) ;
App\Models\Book::insert([
'service_I'd => $service->id]) ;
It tells me that id does not exist on this
Collection instance.
Can someone help me with a better way to achieve this?
in your line:
$services = Service::where('name' 'LIKE' Request::('input')):
it should be:
$services = Service::where('name', 'LIKE', Request::('input'))->get();
or if you want to get similar names:
$services = Service::where('name', 'LIKE','%'. Request::('input').'%')->get();
and in this line:
$service = Service::where('id', $id) ;
you can use:
$service = Service::firstWhere('id', $id) ;
So, to pass the id of the returned collection to the new model, I had to use
///Then,
Book::insert([
'service_Id => $service->id]);
Thanks a lot everyone.
I am trying to update the column with some other data but database table is not letting me update the table because of the NOT NULL constraints in it. I have this option of setting all the fields to NULL but I dont think that will be a good practice. Please I need a solution to it if anyone can help. I get the following error
Illuminate \ Database \ QueryException (HY000)
SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (SQL: insert into users (subject_id, updated_at, created_at) values (?, 2019-07-30 13:46:42, 2019-07-30 13:46:42))
Previous exceptions
SQLSTATE[HY000]: General error: 1364 Field 'first_name' doesn't have a default value (HY000)`
I have tried setting all the values to NULL and it worked but I want to work with some fields setting as NOT NULL and update the ones which are NULL and also if we can fetch or set the fields automatically to what we have ?
This is my controller where I am trying to update the field if this is required or help you understand my problem
public function deleteSubject($id) {
echo $id;
// die();
if(Auth::check()) {
$findSubject = Auth::user()->where('subject_id', $id);
$users = new User();
$users->subject_id = null;
$users->save();
// echo($findSubject);
// die();
Session::flash("message", "You subject has been deleted. You can add a new Subject now.");
return redirect('/subjects');
} else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
The following should work for your code. As it was said in the previous comment, it's because you try to create a new instance of a user without inserting value.
It look like you are trying to delete the subject associate with the authenticated user, so I suppose that you don't really need to create a new user, instead I think you should dissociate the user and the subject. So, the following should work for your code.
The purpose of that variant is to take the authenticated user and put a null value for the subject_id.
public function deleteSubject($id) {
echo $id;
// die();
if(Auth::check()) {
$user = User::where('subject_id', $id)->first(); // This will get the user that have the subect_id, but it's assuming that only one user have this subject_id.
// You can also do this just uncomment the first line below and comment the other one above.
// $user = User::find(Auth::user->id);
$user->subject_id = null;
$user->save()
Session::flash("message", "You subject has been deleted. You can add a new Subject now.");
return redirect('/subjects');
} else {
Session::flash("message", "Please sign in to access this page");
return redirect('/signup');
}
}
I think that you should take a look about how MVC work.
https://selftaughtcoders.com/from-idea-to-launch/lesson-17/laravel-5-mvc-application-in-10-minutes/
You should also take a look at relationship in Laravel: https://laravel.com/docs/5.8/eloquent-relationships
MVC and Eloquent-Relationships will help you understand some function in laravel to achieve this kind of goal really quickly.
If you get a User model and a Subject model, you can simply do something like this:
$user = User->find(Auth::user()->id);
$user->subjects()->dissociate($id);
I'm not sure, but I think the Auth facade let you use the user model function, so maybe this could work to:
Auth::user()->subjects()->dissociate($id);
You should also take a look at middleware: https://laravel.com/docs/5.8/middleware
With middleware, you can put rules like the one you are using to send a message to the user saying that he/she need to be log in to access the page into the middleware and reusing it whenever you need.
I want to delete updated value from db. Like if the notice is inserted of notice column in class table and I want to delete it. So what will be the query in yii2? Please help -
$model = Class::find()->where('id', $id)->one;
$model->delete();
Above query is for -
DELETE FROM class WHERE id = $id;
I want to write query like that in yii2 -
DELETE notice FROM class WHERE id = $id;
If you are searching using the primary key, you can use
$model = Class::findOne($id);
Then, for deleting (setting to null) the notice field, you just need to use
$model->notice = NULL;
$model->save();
Note that if the notice attribute is required, or not null, the save will fail.
Yii allows you to execute SQL command direct
https://www.yiiframework.com/doc/api/2.0/yii-db-command
$query = "DELETE notice FROM class WHERE id = {$id}";
Yii::$app->db->createCommand($query)->execute();
Greetings,
Facts:
Database named -> acastro
Table called -> contacto
Fields in table are -> id, nome, email
I making an Yii2 application, and need to connect a highcharts chart to a table field in my database.
How can i inside an action called actionAdmin connect to my database and then count the number of id's in my Contacto table stored inside acastro database.
In the old Yii1.xx i used to establish connection this way:
public function actionAdmin() {
$sql = Yii::app()->db->createCommand('
SELECT count(*) as total
FROM contacto
')->queryAll();
$total = array();
for ($i = 0; $i < sizeof($sql); $i++){
$total[] = (int) $sql[$i]["total"];
}
$this->render('admin', array('total' => $total));
}
}
The problem is that this syntax no longer works in Yii2, and i've tried the sintaxe explained in Yii2 api guide but it always give's me error of undefined variable. Here is the code that i'm using to connect acording to Yii2 api guide:
use yii\db\Command;
$total = $connection->createCommand('SELECT count (*) FROM contacto')->queryAll();
What am i doing wrong ? Any solutions ?
Many thanks in advance.
I am not very sure that it will solve ur problem.
But in yii2 this the syntax
use app\models\Contacto; //look your Contacto Model namespace
$query = (new Query())->from('contacto');
$count = $query->count('column_name');
I hope this will help
The easiest syntax in Yii2 is:
$count=(new \yii\db\Query)->from('TBL_NAME')->count('*');
It just returns the count. For example: 500
I have the code to insert the items into to an order as follows,
$ordId = 1;
$prodId = 221;
$type = '';
$price = 1000;
$qty = 5;
$desc = DESC;
$notes = "test notes";
$test = $app->addOrderItem($ordId, $prodId, $type, $price, $qty, $desc, $notes);
Is there any functions/methods available for inserting the orders directly into Infusionsoft?
If you're using the InfusionSoft PHP SDK, you have a couple of options. (1) Use the OrderService API's placeOrder() function, (2) Use the InvoiceService API to create a blank invoice, add line items to that invoice, and charge it. The OrderService API is a good one-off option; but the InvoiceService allows much more flexibility.
Using OrderService.placeOrder
$order = $app->placeOrder(
(int)$contactId,
(int)$creditCardId,
(int)$payPlanId,
(array(int))$productIds,
(array(int))$subscriptionIds,
(bool)$processSpecials,
(array(str))$promoCodes
)
NOTE: The OrderService API requires you to have already added a Contact via the ContactService API, and a Credit Card via the DataService API (by adding it to the CreditCard table).
You can use the order service. Details can be found here http://help.infusionsoft.com/api-docs/orderservice
check the complete code here
$app = new iSDK();
// perform authorization tasks
$carray = array(
$app->key,
$contactId,
$creditCardId,
$payPlanId,
array($productId1, $productId2),
array($subscriptionPlanId1, $subscriptionPlanId2),
$processSpecials,
array($promoCode1, $promoCode2)) // array of strings
);
$app->placeOrder($carray);