I am using CouchRest for Rails and I'm having problems understanding the documentation (or lack of it).
response = #db.save_doc( { :key => 'value', 'another key' => 'another value' } )
#doc = #db.get(response['id'])
doc = #db.get(:key => 'value')
After saving the doc, how do I get all the documents where the key => 'value' =
thanks
You need to create a view where 'key' is your index. Then you can query that view.
Based loosely on the docs:
#db.save_doc({
"_id" => "_design/my_view",
:views => {
:test => {
:map => "function(doc){ emit(doc.key,null)}"
}
}
})
puts #db.view('my_view/test', 'key' => 'value')['rows'].inspect
Related
I'm doing cakephp4 controller test with phpUnit but when I call save my id auto_increment disapear.
My table before save():
Image before save
The test:
public function testAdd(): void
{
$this->session([
'Auth' => [
'id' => 1,
'DNI_CIF' => '22175395Z',
'name' => 'Prueba',
'lastname' => 'Prueba Prueba',
'username' => 'Pruebatesting',
'password' => 'prueba',
'email' => 'prueba#gmail.com',
'phone' => '639087621',
'role' => 'admin',
'addres_id' => 1
]
]);
$this->get('animal/add');
$this->assertResponseOk();
$data=[
'id' => 1,
'name' => 'AñadirAnimal',
'image' => '',
'specie' => 'dog',
'chip' => 'no',
'sex' => 'intact_male',
'race' => 'cat',
'age' => 1,
'information' => 'Es un animal.',
'state' => 'sick',
'animal_shelter' => [
'id' => 1,
'start_date' => '2022-11-03 10:47:38',
'end_date' => '2022-11-03 10:47:38',
'user_id' => 1,
'animal_id' => 1
]
];
$this->enableCsrfToken();
$this->post('animal/add',$data);
$this->assertResponseOk();
}
The controller:
public function add()
{
$animal = $this->Animal->newEmptyEntity();
if ($this->request->is('post')) {
$animal = $this->Animal->patchEntity($animal, $this->request->getData());
if(!$animal->getErrors){
$image = $this->request->getData('image_file');
if($image !=NULL){
$name = $image->getClientFilename();
}
if( !is_dir(WWW_ROOT.'img'.DS.'animal-img') ){
mkdir(WWW_ROOT.'img'.DS.'animal-img',0775);
if($name){
$targetPath = WWW_ROOT.'img'.DS.'animal-img'.DS.$name;
$image->moveTo($targetPath);
$animal->image = 'animal-img/'.$name;
}
}
if ($this->Animal->save($animal)) {
$this->Flash->success(__('El animal se ha añadido.'));
return $this->redirect(['action' => 'index']);
}
}
$this->Flash->error(__('El animal no se ha podido añadir, por favor intentalo de nuevo'));
}
$allUsers = $this->getTableLocator()->get('User');
$user = $allUsers->find('list', ['limit' => 200])->all();
$this->set(compact('animal','user'));
}
My table after:
Image after save
The error:
1) App\Test\TestCase\Controller\AnimalControllerTest::testAdd
Possibly related to PDOException: "SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value"
…
Failed asserting that 500 is between 200 and 204.
I don't know why this is happening or how to know the reason. In the app the controller works fine. Data in the app:
Data in app
Data in test:
Data in test
I hope someone can help me, I don't know what to try anymore or how to know where the problem is...
I tried to look at the data but it doesn't apear to have any errors so I don't know where the error can be.
It was that the sql file used in the bootstrap didn't have the autoincrement value.
Good night: I used to create node programmatically with a code similar to:
use Drupal\node\Entity\Node;
$nodeObj = Node::create([
'type' => 'article',
'title' => 'Programatically created Article',
'body' => "CodeExpertz is contains best Blogs.",
'field_date' => '2017-10-24',
'field_category' => 'Study',
'uid' => 1,
]);
$nodeObj->save(); // Saving the Node object.
$nid = $nodeObj->id(); // Get Nid from the node object.
Print "Node Id is " . $nid;
Now I want to create entities content (no nodes) but I can't find something about this. I tried to adapt the next snippet:
$term = \Drupal\taxonomy\Entity\Term::create([
'vid' => 'test_vocabulary',
'name' => 'My tag',
]);
$term->save();
to this (vehicle is my entity):
$newvehicle = \Drupal\vehicle\Entity\Vehicle::create([
'title' => 'Ferrari'
]);
$newvehicle->save();
The result is the white page of death.
Thanks for your help.
I was able to do it with this code
use Drupal\customModule\Entity\entityCustom;
$entityCustom = entityCustom::create([
'type' => 'custom_entity',
'uid' => 1,
'status' => TRUE,
'promote' => 0,
'created' => time(),
'langcode' => 'en',
'name' => 'NAME',
]);
$entityCustom->save();
Working on a project in Backpack for Laravel that involves uploading images, videos, etc. Starting with a simple image upload, I have a mutator in my model as follows:
public function setThumbnailAttribute($value)
{
$attribute_name = "Thumbnail_URL";
$disk = "s3";
$destination_path = "images";
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
But it doesn't seem to fire, and whenever the file is 'uploaded' it shows a 'C:\Windows\Temp\php6803.tmp' as the location.
My field:
$this->crud->addField([
'name' => 'Thumbnail',
'label' => 'Thumbnail',
'type' => 'image',
'upload' => true,
'disk' => 's3'
]);
And my 's3' disk in filesystems.php:
's3' => [
'driver' => 's3',
'key' => env('AWS_KEY'),
'secret' => env('AWS_SECRET'),
'region' => env('AWS_REGION'),
'bucket' => env('AWS_BUCKET'),
],
I've double checked that the Thumbnail_URL is fillable. I'm really not sure what I'm missing.
public function setThumbnailAttribute($value)
{
$attribute_name = "Thumbnail_URL";
$disk = "s3";
$destination_path = "images";
**MISSING** $this->attributes[$attribute_name] = '....' (image path/filename)
$this->uploadFileToDisk($value, $attribute_name, $disk, $destination_path);
}
I am trying to integrate omnipay into a website. The firts time I wanted to create a card, I came across this problem:
Omnipay: InvalidRequestException "The source parameter is required"
Here is my code:
$gateway = Omnipay::create('Stripe');
$gateway->setApiKey('sk_test_4IHf5iPTXVaZ8SF5GDcLTrqY');
$name_arr = explode(" ", $this->req['card-name']);
$card_data = [
'firstname' => $name_arr[0],
'surname' => $name_arr[1],
'expiryMonth' => $this->req['exp-month'],
'expiryYear' => $this->req['exp-year'],
'number' => $this->req['card-number'],
'email' => $client['email'],
'cvv' => $this->req['cvv']
];
$response = $gateway->createCard($card_data)->send();
What am I missing, or doing wrong?
Thanks!
Ok, sorry, I have found the solution! Data has to be in the following format:
$card_data = ['card' =>[
'firstname' => $name_arr[0],
'surname' => $name_arr[1],
'expiryMonth' => $this->req['exp-month'],
'expiryYear' => $this->req['exp-year'],
'number' => $this->req['card-number'],
'cvv' => $this->req['cvc']
]];
i am newbie in cakephp and trying to implement this query
SELECT DISTINCT textmessage.mobileNo FROM textmessage
JOIN contacts
ON textmessage.User_id=contacts.User_id AND textmessage.mobileNo = Contacts.mobileNo
i am expecting only one result here .. want to implement this query in textMessage Controller ... i have never use join query before in CAKEPHP... i have actually a mobileNo field in both the tables and i want to retrieve the mobileNo if the mobileNo of textmessage table is also in Contacts table
here is what i have modified your query according to my requirments ..
$this->bindModel(array('belongsTo' => array('Contact' => array('className' => 'Contact',
'foreignKey' => false,
'conditions' => array('Message.user_id = Contact.user_id','Message.mobileNo = Contact.mobileNo')))), true);
return $message_details = $this->find('all', array('conditions' => array(),
'fields' => array('DISTINCT mobileNo')));
Put the following code in your controller's code:
If you need only single record:
function test1()
{
$this->TextMessage->bindModel(array('belongsTo' => array('Contact' => array('className' => 'Contact',
'foreignKey' => false,
'conditions' => array('TextMessage.user_id = Contact.user_id')))), false);
$message_details = $this->TextMessage->find('all', array('conditions' => array(),
'fields' => array('DISTINCT mobileNo')));
}
If you have multiple text messages corresponding to each contact then try the following:
function test2()
{
$this->Contact->bindModel(array('hasMany' => array('TextMessage' => array('className' => 'TextMessage',
'foreignKey' => false,
'conditions' => array('TextMessage.user_id = Contact.user_id'),
'fields' => array('DISTINCT mobileNo')))
), false);
$message_details = $this->Contact->find('all', array('conditions' => array()));
}
You can also write the association in your model. I gave you an example to dynamically join any table on the fly.
According to your edited question, if you don't need two arrays. Use query() function.
Hope this will fulfill your requirement.